How to kill the main thread? - c#

Strange issue. I kill the main thread, but when the program ends, it still processes the code after this.Close().
Any ideas on how to stop that?
MessageBox.Show("Servers overloaded. Exiting.");
this.Close();
//...other code that processes even after it closes...

Closing the window does not stop the thread.
You can use Environment.Exit to shutdown immediately.

Unlike ASP.Net's Response.End(), calling Close will not abort the thread.
You need to add return; after calling Close() to exit the method.

Why would it stop at Close()? It is still going to exit your method - most things like Close() are going to involve sending something to the windows message queue and having it processed. If you need to exit NOW, then perhaps Environment.FailFast...

Related

Do I have to close running BackgroundWorkers when user closes form?

From here Does closing the application stops all active BackgroundWorkers? it seems not.
But from here How to stop BackgroundWorker on Form's Closing event? it seems yes.
So which is it?
(EDIT: I realize that the BackgroundWorkers might exit with an exception. But what's the problem with that? Isn't the point here to not leave running threads which take up resources?)
Closing a Form does not stop all background workers started by that form.
When the entire application ends it will stop all background threads.
Closing the main form (unless you have modified the Main method to do something else) will end the entire application.
Each question you referenced is correct for what it says. If you close the main form, then the entire application will end and the background worker will be closed on its own. If the form that is closing isn't the main form, but some other form, and you want the background worker that it starts to be stopped, then you will need to do so yourself.
It's also worth noting that the second link that you have provided asks for something a bit more complex. It's clear in that post that closing the form (if it's the main form) will stop execution of the background thread. What the OP is trying to do there is to tell the background thread, "hey, it's time to finish up, we're done here" and then have the form wait until that background thread can finish cleaning things up nicely, rather than just exiting and forcibly aborting the thread while it's in the middle of doing something.
Both of those links that you provide have the correct answer- BackgroundWorkers will be closed when the program is closed. Unmanaged resources are the ones you have to worry about explicitly closing.

Does Application.Exit() kill background threads?

I created a form which runs multiple background threads. I added another class which handles exceptions and errors if any. When the code runs, if an error occurs that should force the application to close can I just use Application.Exit()? Will that kill the background threads as well?
No, it will not. Application.Exit() will simply force the windows message pump to post a Quit message, which will terminate your application's main thread. However, the process itself will continue to run until the background threads complete.
Update: as commenters have correctly pointed out, if your thread's IsBackground property is set to True, terminating the main thread via Application.Exit() will shut down the process.
No. Thread termination is cooperative.

C# is there anyway to instruct a code block to finish processing before the app gets shut down?

I know this won't work in all scenarios, but please keep in mind the following 3 scenarios:
An IIS reset - if the code is running inside IIS
A Server restart or shut down
User closes the app (if its a Windows form or Console App).
Lets say I have a code block that runs a loop. Is there a way to ensure at least that the current loop item gets processed before the app shuts down.
Like this...
Loop runs: 100 items, app gets shutdown (for reasons above), app is busy with item in loop 53 for example. It first finishes all code for that item between the foreach... and then allows the app to gracefully shutdown.
Is this type of thing possible?
Nothing I would do but If it is ok to abuse the system you might be able to use the CriticalFinalizerObject
It is guaranteed to execute
even in situations where the CLR forcibly unloads an application domain or aborts a thread
I really don't think so. You are stuck in front of windows. Windows take that kind of decisions for you.. IF somebody is shutting down the pc, then you are just shut down. This is the same scenario as if they where a power failure. What will you do in that case?
For a normal application: While your code is running in a foreground thread (not ThreadPool or Thread.IsBackground == true) it will not be aborted mid-execution, unless the user forcibly quits the process.
If you are running your loop in a background thread, you can try handling the exit event of the application, waiting for the loop to finish or at least reach a stable state before being aborted.
In most apps you can handle close event and not allow it but you can't do anything when user decides to kill your process. So i would say that its not possible.
How about doing this. using a try catch and finally in the static void Main
Here even if you end task the application, finally will run. not sure about power failure.
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try
{
Application.Run(new Form1());
}
finally
{
MessageBox.Show("I run always.");
}
}
Hope it helps

Killing all threads that opened by application

I have some really big application mixture of c# and j#.
Sometimes when I close it, there are some threads that are not closed and they are hanging in the task manager and it is impossible to kill them from there.
I have really problem to find all those threads and add them to the closing event .
Is there some way violently kill all threads that were opened by application in the closing event ?...
Thanks.
Is there maybe some Tool that can tell me what threads are opened while i close the application and who openned them ?
This shouldn't be happening, and if it is, you're trying to address it the wrong way.
When your application exits, the .NET Framework will automatically kill any threads whose IsBackground property is set to "True". Designate each of your worker threads as background threads, and you won't have this problem anymore. Taking advantage of the BackgroundWorker class and the ThreadPool class, which automatically create background threads, is a much better option.
Otherwise, you need to clean up your foreground threads explicitly. A properly designed application will do its own bookkeeping and have a deterministic, structured way of ensuring that all its threads have been closed before exiting the Main method. This is what you should be doing anyway if your threads require a graceful termination.
Killing the process is a very bad idea, as is letting your threads run about willy-nilly in your application.
You can use : Environment.Exit(0); , that will shutdown the application if threads are running and wont cause any problems.
You should close your threads gracefully, but just want you and others to know the way that is not recommended but possible:
on your OnClose Handler:
System.Diagnostics.Process.GetCurrentProcess().Kill();
I totally prefer Cody Gray way of doing it.
Well, you could call Application.Exit() but that won't be of much help.
The bottom line is that you have to gracefully close all of the threads yourself if you want to do things properly.
My 2 cents... to all answers...
Try to force SHUTDOWN
Put into void CurrentApplication_Exit(object sender, ExitEventArgs e) and private void Window_Closing(object sender, CancelEventArgs e) those lines
System.Windows.Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
System.Windows.Application.Current.Shutdown();
internal void Close()
{
Dispatcher.CurrentDispatcher.Thread.Abort();
Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
Application.Current.Shutdown();
}
I was having similar issue while closing form/application. It was not really exiting from the debug mode and comes to the design mode. Following resolves my issue.
Followed the link.
Windows Form Application, Thread won't stop
Step one did not resolved it.
Environment.Exit(0); - Resolves and worked fine.

Application.DoEvents();

How do I keep my C# form that, lets say is in a for-loop, from locking up? Do I call Application.DoEvents(); before the loop or after? From what I've heard using the DoEvents method will keep my app from locking.
You should not use Application.DoEvents() in order to keep your application responsive.
Calling this method will allow any waiting windows messages to be dispatched. This means if a user clicks on a button (or performs any other user interaction) that action will be processed. This can therefore cause reentrancy. If they press the same button as the one that caused the loop you are processing you will end up having the routine called again before you have finished!
Instead you should use a BackgroundWorker thread to perform the long process and then once the action is completed perform whatever additional actions are required. For example, once a button is pressed you would start the worker thread and then disable you button so it cannot be pressed again. Once the worker thread completes you would enable the button again.
There are a few ways, this (DoEvents) just forces the message pump to process messages. Some people put a Thread.Sleep at the end of a loop (always inside the loop though) in a thread. What exactly are you doing in your thread, because there might be a better way to accomplish your goal overall?
If the process is causing the UI to lock up for an unacceptable amount of time, try using a seperate thread (either create it, use the thread pool, or use the BackgroundWorker class.

Categories

Resources