I've got the following code:
https://github.com/YatoDev/Yato.LowLevelInput/blob/master/Yato.LowLevelInput/WindowsHooks/WindowsHook.cs
I am using this for LowLevelMouse and LowLevelKeyboard hooks.
When i debug the application and pause it, the hook wont execute.
This results in input lags for at least 10 seconds
I also tested it using CreateThread instead of a managed thread (also with the HideFromDebugger flag)
Sadly, it does not help.
It seems like the c# debugger freezes any thread that executes managed code.
I wonder if there is a way to exclude my hook threads to prevent them being suspended.
Note: I searched for this problem but i did not find an answer providing an automated solution. I hope to find a way to exclude them by Thread.Name.Contains("Foo") or by setting up the debugger to never suspend all threads.
Related
Is there any tool which gives you the view of current execution of you code? I need this because my WPF application's UI is hanging without any notification/exceptions. I am not able to debug the issue. System stays Idle and every time I have to kill the process explicitly. Any suggestions. FYI.. many third party tools are being used.
I am not sure what you have tested already and maybe I answer is invalid but:
Have you tried to use the pause function in Visual Studio. It pauses all threads of your application and you can see per Thread where it is paused. You can also step through each Thread to find possible issues.
Some issues I had in the past:
Too much actions where executed immediately from the UI Thread
Dispatch big chunk of work to background threads using the BackgroundWorker
Incorrect locking model so that the UI thread is not able to gather the information it wants
Make sure data in the ViewModel cannot be changed by background threads without locking properties.
Some interesting links to read about WPF and MultiThreading:
WPF Fundamentals - Threading Model
Debugging locks and deadlocks
Hopefully this helps you a bit
You can use windbg to fully investigate all aspects of a process. While there is a learning curve involved, in my opinion, becoming familiar with this tool is vital for a developer. To get started get a full memory dump of your running process via task manager & download & install debugging tools for windows. Once you've this done take a look at some tutorials & I'll try and help if you post a comment back
If you are doing Memory/CPU Consuming tasks using the main thread the UI will become Idle even without any exceptions.To solve this issue use a BackgroudWorker/a Separate thread to do the time consuming operations.So that UI will not freeze.
I am doing a project where I am loading several assemblies during runtime, for each of those assemblies I use reflection to find some specific classes, instantiate them and calling their methods. All this is working fine, but for some of the calls the process encounters a stack overflow which terminates my entire program. I don't have any control over the source code of the assemblies I am loading so I cant change the code I'm executing.
What I have tried to solve the problem:
I assign a thread to do the invocation of the methods and tried to
abort the thread after a timeintervall(I know that this is bad
practice but I cant change the code to terminate friendly). This
however doesn't work, I think the thread is to busy "stackoverflowing"
to handle the Abort-call.
Ive tried reducing the actual memory the thread has access to, this is not even a solution because you cant catch the stackoverflow-exception so my program terminates anyway (just quicker)
Questions:
Can a thread be to busy to be aborted? Is there some way to abort a thread that is having this behaviour?
How can we call code (that we don't have any control over) in a good way?
Thanks in advance!
The recommended procedure in case of "opaque code" is to actually fork a new process and start it. That way you gain two benefits:
If it fails by itself, it's isolated and won't take your main application down as well.
You can safely kill it and it won't cause as much trouble as an aborted thread.
I have one background thread I use for processing separately from the GUI, and everything works fine between the threads. However when I close the application, the program "closes" but does not kill the process. The background thread is keeping the program alive it seems.
I have set "myThreadInstance.IsBackground = true;", and I thought this would require C# to kill it when it is killed.
I am testing this all in Visual Studio (2010, using .NET 4.0), and after the first build, the rest all fail because the exe is still in use so it cannot overwrite it. Looking in task manager, it is there. Killing Visual Studio releases the vbhost process which releases my exe. Killing my exe's process, makes vbhost respawn it in a second or two.
Actually, based on your description and other things you've tried (and their results), I believe the most likely cause is this:
You have another foreground thread in your application, other than the one you're examining.
Try using Application.Exit(0); in the form_closing/form_closed event.
Bug: I think this might be a bug. Look at the comments at the bottom of this MSDN page: http://msdn.microsoft.com/en-us/library/system.threading.thread.isbackground.aspx
Also, try using the BackgroundWorker. Here's a good description in the VisualStudio Magazine: http://visualstudiomagazine.com/articles/2007/09/01/simplify-background-threads.aspx
This type of problem usually requires code to figure out. Take your app and trim it down to the bare minimum necessary to show the issue.
However, most likely you are either not signaling the thread to terminate or the thread is such a long running beast that it doesn't ever see the signal.
I am developing a multi-thread application and one of my threads is somehow blocked by something and thus it will occupy some resource such as a file forever. Is there a way to find out which thread is being blocked and more important what resouce is being held by that thread?
At last and as usual, thanks for your patience and reply. :)
---------new progress----------
Since all the resouces on the windows platform are accessed via "handles". I am thinking if there is a way to list all the handles a .NET thread is holding?
When you Create the Threads, you can Set a Name for each thread just before you start it. You can then use these in the Debugger to identify the Thread in question.
Then when your program doesn't close, you jump up to the debugger and Hit Pause, you can then via the active threads. Debug->Windows->Threads (CTRL-ALT+H)
If your threads are not critical, and you just want the system to Close upon Completion you can set .IsBackground on the thread and you don't need to perform any special operations to get your thread to close, often though this may lead to other conditions you were not expecting.
I personally also use System.Threading.AutoResetEvent(duration) instead of Sleep(duration) because if I wish to exit, I set mIsRunning=false then set the event which will cause the Stopping of the Event to Wake up, but Exit Immediately. A Sleep has to finish before it resumes.
The new Concurrency Profiler in Visual Studio 2010 was built to answer and solve this type of question.
Resource Contention Concurrency Profiling in Visual Studio 2010 – Performance Investigation Flows
I recommend downloading the trial version if you don't already have Visual Studio 2010. You won't be disappointed. :)
If you are familiar with dump analysis using WinDbg, you can also capture a hang dump when this issue happens, and then manually analyze the dump to see which thread hangs and why.
http://blogs.msdn.com/tess/archive/tags/Performance+issues+and+hangs/default.aspx
I am developing a WinForm multithreaded application with C#.
Sometimes it happens that my application hangs, or freezes or blocks.
When this happens and I am running in DEBUG mode, is there anyway to understand at what line of code my application is currently? Since it is freezed I expect to find a point where the application is locked or blocked or whatever.
Is it possible to do that someway?
When It is freezed I tried to open the CALL STACK window, but this doesn't display any info; is there some action that I might do? Some "pause and check" or whatever?
You may need to open the Threads window, and change the current thread. While debugging, choose Debug->Break All, and open the Threads window. If you go through each thread, by double clicking on the thread, you should be able to investigate the call stack for each thread.
That being said, if you can run your program in VS 2010 - this gets a lot easier. In VS 2010, you can use the new Concurrency Profiler, and run your code under the Concurrency Profiler with the option to Visualize the Behavior of a multithreaded application. Once your application locks up, kill it, and let the profiler churn -
Eventually, you'll get a diagram which shows each thread in your program, and when they're locked. The callstack for each blocked thread will be shown, as well as what lock is being held (with the line of source code). This makes it very easy to track down a dead lock.
When debugger is attached go to Debug menu and choose 'Break All'... Then you can examine call stacks for all thread.
You could press pause and see where it ends up (use the aforementioned call stack window). Chances are however that you'll end up in native code. You could try to step out a bit or just look at the stack for a managed function to debug.
Alternatively you could put a breakpoint after the application "freezes" and try to pinpoint a loop that doesn't end.
Both the above are assuming your application is busy (100% cpu usage). If your application is however stuck in a dead lock or just plain waiting for a mutex that won't tick, you'll have to manually re-read your code and try to figure it out on your own.