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

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.

Related

Process running on computer after application exit

I have a question, I created an application on visual studio using C# and Window Forms, but every time it runs, if the application close (either manually by the person or when it reach the end) it continue running on the process of the computer, so if I open and close the application 3 times there will be 3 processes with the same name activated. How can I prevent that from happening? So far the only way for me to close it is going to Window Task Manager and closing it manually, which is a pain...
Any ideas?
if you want to kill all processes and exit from application then first you need to kill threads in background
Application.ExitThread();
and then exit from application
Environment.Exit();
You can use Saif's answer which forces background threads to abort, but I'd recommend you manage your threads better. If you have threads that run for an extended period of time, you should have a flag (a boolean that can be accessed from anywhere in your code) that tells the threads they should stop running. This is a safer method than
Application.ExitThread();
because it allows you to flush and close streams, disconnect your socket connections or tidy up whatever you're doing in your threads.
Well, based on this problem you can check if your application is running on your application startup. That way you only start the process if it is not running:
I´m going to check for example notepad:
Process[] pname = Process.GetProcessesByName("notepad");
if (pname.Length == 0)
{
//The application is not running. Start your process here
}
else
{
//Your application is running. Do nothing
}

threading in c# windows service

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.

How to force BackgroundWorker to continue running when app is closed?

I don't know how much about BackgroundWorker, and I have a problem. My BackgroundWorker is correctly running to completion when my app is open, but if my app is closed the BackgroundWorker does not complete.
Is there any way to force a BackgroundWorker to continue running when my app is closed, rather than the BackgroundWorker terminating?
Any task launched by your application will stop executing when your app is closed, and there's nothing you can do against that (well, there's a few very specific exceptions, like geolocation apps).
If you need to run code while your app is closed, you need to use a background agent. Note that those agents have a few limitations, especially in memory usage, and can run only for a few seconds every 30 minutes.
You will need a Background Agent
http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202942(v=vs.105).aspx

Form is running on processes even after close the program

I was created a C# program using 2 forms. I add both of them a exit button with close(); method.But when i run the executable file , even if i close the program with exit button still the program run on processes at task manger. I believe its something related with one form closing but the other one was loaded to the memory and not stopping. Such situations. how come we terminate the complete program without it not storing at memory? close(); method not enough?
There are many ways this could occur. Some of the more common include:
If you use Application.Run() (instead of Application.Run(someForm)) to start the application, it will run until you call Application.Exit or use another method to shut it down. Closing the forms will not shut down the application
If you start a thread, via new Thread, and don't make it a background thread, that will also keep the program alive. Processes will stay alive until all foreground threads have completed. You can work around this by making the thread a background thread (or using the Task class instead of a Thread).

Make sure all threads are closed or forcibly close a thread?

I a WPF/C# application that has a psuedo-process of
Click a button > Start a thread to take a picture from a web cam API > Instantiate the web cam API > API starts a callback thread > Picture is taken > API object is disposed
These steps generally work except for the last portion where the callback thread to the web cam API does not close out. So, in my Task Manager, I invariably end up with a "ghost" process, which shares the same name as my base WPF application. Also, making a second call to the same web cam API (to take a second picture) fails miserably.
So, I'm trying to find a way to make sure that all of my threads from my root application are forcibly closed out at all times. Is there a way to ensure that no threads are left over?
If you set IsBackground to true on those threads they will be terminated at shutdown.
If you have a window in your app, set the Application.ShutdownMode to OnMainWindowClose. That will terminate any running background threads for you when the main window closes. Programatically you can call Environment.Exit to kill all threads as gracefully as possible (but forcefully if not) and exit the application.
If you see some ghost process besides your main program, it is not a thread problem. You've created other process to perform the operation. Kill it. Your resources will be released. You don't have to worry about threads being running when there is a lot bigger problem - other process hanging on to resources.

Categories

Resources