threading in c# windows service - c#

In a windows service project in c# I create 2 threads to execute 2 jobs. When looking at the process explorer tool I see more than 30 threads created for the process. Debugging through my code I don't see more than 2 threads being created. I'm not sure why I'm seeing so many threads for the process in the process explorer.
The threads are supposed to run every 2 minutes so I call Thread.Sleep(time).
Any ideas why I'm seeing so many threads in my process explorer?
The jist of what I'm seeing is shown below, but instead of 4 threads I'm saying a whole lot more:
Process PID Type Name
MyService.vshost.exe 1234 Thread MyService.vshost.exe(1234) 1265
MyService.vshost.exe 1234 Thread MyService.vshost.exe(1234) 6528
MyService.vshost.exe 1234 Thread MyService.vshost.exe(1234) 3175
MyService.vshost.exe 1234 Thread MyService.vshost.exe(1234) 5325
Thanks in advance.

VS doesn't show the other threads because the the debugger is attached to the managed code but most of those threads run native code only.
For attaching to the native code, open Attach to Process window, click Select and in the Select Code Type window select Native as well as Managed.
Now you can see all the threads in the threads window: managed and native.

Related

How to know which method started a Worker Thread

I'm developing a c# multithreaded application since some years. A few days ago I've noticed that, while it runs, it continuously creates new worker threads (almost once per second). Using Visual Studio 2013 "Threads" window these threads have No Name and Location is "not available", so I cannot check where these threads start.
I gave a name to all threads generated in my code, but still these threads are created with "no name" so I guess that they may be generated by an external assembly.
My application integrates NLog.dll (for logging into txt files), System.Data.SQLite.dll (for data reads and writes to db3 databases). It also uses UDP and TCP sokets for data exchange.
Example:
After 2 minutes of application running I have 70 threads. Then, after 20 minutes the number of threads is 190!!! If I check Threads window in Visual Studio I see that all new threads ahave "Category" "Worker Thread" and "Name" is "No Name".
Here a snapshot of my Visual Studio Threads window:
Can anyone suggest me how to find the origin of this threads flooding?
Thanks in advance!
I would try Process Monitor from SysInternals toolkit (www.sysinternals.com/).
ProcMon has a filter for thread activity events.
This kind of threads (with Name "No Name" and Location is "not available") are created by Timer object (in my case System.Windows.Forms.Timer). The thread flooding was caused by this line of code:
timerUpdateStatus.Change(0, 0);
that was called in the Tick event handler and then resetted back to the correct value by another call to Change method:
timerUpdateStatus.Change(statusUpdateTime, 0);

how to close all background threads of a Windows Service application ?

I have created a windows service application , In that i open about 10000 threads , I want to close all threads just like all background threads get closed when I close winform applications , how can I accomplish this ? i don't want to store reference to all these threads in an array and close all by using a loop.
Before I used a Winform application and when I closed the winform application all backgrounds threads get closed itself. I want to accomplish something like that to close all threads in Windows Service application
Any idea will be very appreciated.
If you set the thread property myThread.isBackground = true, the thread will die as all the foreground threads are killed (when you close the application).
However, creating 10 000 threads is not a good thing as the operating system will spend more time switching between the threads than actually executing the threads' code. Executing that much thread on normal computers will actually be less efficient than executing 2 - 16 threads (for example) on a 2 - 4 cores CPU.
Here's an excellent e-book on threading.
PDF : http://www.albahari.info/threading/threading.pdf
Web : http://www.albahari.com/threading/
I can see two options to achieve your goal :
Have your Service Application to start another Executable program that is responsible for starting your threads. Then have your service application to kill that Executable program when needed. The threads spawned by the Executable program will be killed but not the service.
Put a break condition in your running thread. Through setting a boolean flag you could have your threads exiting the while (true) and exit the Runnable delegate. (Here I assume your threads are constantly running in a while (true), otherwise you would not ask that question).
If you just handle the stop message and do nothing else, the OS should stop all the remaining threads in the service process after ~20 secs.

What is causing this application to be hung (likely on native side)

I have a .net managed application that interops with a native dll. Problem is that sometimes the application just hangs and doesn't respond. Looking at the managed side, I don't see any threads callstacks doing any wait or sleep. SOS !threads and !syncblk does not show any locks either. Any idea what should I look into the native side to figure out what is been blocked etc?
Try going to
Task Manager -> Performance -> Resource Monitor...
right click your process and click
Analyze Wait Chain...
This could give you some information.

How to see background threads in Visual Studio 2010 debugger

I'm trying to figure out why the application process lingers in Task Manager after the application is closed and the window disappears.
When I get VS to attach to the zombie process and break all, the threads window shows that the main thread is still alive, and a number of worker threads as well.
Some questions:
Are worker threads necessarily background threads? If not, how do I identify the background threads as I didn't see such a column in the window?
Do I simply double-click on each thread in the thread window, and watch the Thread.IsBackgroundThread value?
When I click on the main thread, the debugger doesn't show a call stack. How
do I identify where the main thread is stuck?
I would strongly sugest you tu use WinDbg. It is not a visual debugger though it is much more powerful.
Surely I will figure you out.
To list all threads in a process use: ~.
To switch to a certain thread ~thread_ids.
To see stak of curent thread !clr_stack.
Brief tutorial.
http://www.codeproject.com/KB/debug/windbg_part1.aspx
Also try in google "Debuging Asp.net with windbg"

Debugging Windows Form Thread Freezes

I have a C# winforms application, which communicates to various com data sources, and uses a threadpool for most of its backend processing. I have noticed that 2-3 times a day the winforms thread hangs for 20-30 seconds (visible in the ui, and that the com data stops for 20-30 secs). I have since written a simple task on the threadpool that tracks a heartbeat on the winforms thread to detect these instances, but am looking for a way to automatically trigger a full dump (not a mini dump), so that I can see what exactly the winforms thread is doing during these pauses.
Are there any simple command line apps that my background thread can call on it's own process to bind to the app as a debugger, generate the full dump file, and then allow the application to resume?
Is there a better way to debug this?
You can use the SysInternals procdump utility to generate dump files:
ProcDump is a command-line utility
whose primary purpose is monitoring an
application for CPU spikes and
generating crash dumps during a spike
that an administrator or developer can
use to determine the cause of the
spike.
Sounds like Process Dumper should do the trick.

Categories

Resources