Form is running on processes even after close the program - c#

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).

Related

Environment.Exit(0) or Application.Exit() after all, which to use? [duplicate]

Following are the ways by which we can exit an application:
Environment.Exit(0)
Application.Exit()
Form.Close()
What is the difference between these three methods and when to use each one?
The proper method would be Application.Exit(). According to the Documentation, it terminates all message loops and closes all windows thus giving your forms the possibility to execute their cleanup code (in Form.OnClose etc).
Environment.Exit would just kill the process. If some form has e.g. unsaved changes it would not have any chances to ask the user if he wants to save them. Also resources (database connections etc.) could not be released properly, files might not be flushed etc.
Form.Close just does what it says: it closes a form. If you have other forms opened (perhaps not now but in some future version of your application), the application will not terminate.
Keep in mind that if you use multithreading, Application.Exit() will not terminate your threads (and thus the application will keep working in the background, even if the GUI is terminated). Therefore you must take measures to kill your threads, either in the main function (i.e. Program.Main()) or when in the OnClose event of your main form.
they are all fine.
but form.Close() won't close your application
it closes the form and after that
the main-method returns an int (exitcode).
if you want that your application exits with exitcodes use
Environmet.Exit(exitcode) or return the exitcode in the main-method

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

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.

Application process not ending when I close the form? (C#)

Experimenting with the TcpClient and TcpListener class and for some reason when I have a couple of threads running and I close the form the process does not end but the form disappears.
I have to manually kill the process with the VS IDE or task manager.
Nothing in the form is still running from what I can tell when I close the program but the process does not end.. I insert breakpoints everywhere and even the console output says the threads exited.
Anyone know what's going on here?
The main thread of your application is waiting for the threads your spawned to finish. You can set the IsBackground property of your threads to true so they do not stop your process from terminating:
From MSDN:
A thread is either a background thread or a foreground thread.
Background threads are identical to foreground threads, except that
background threads do not prevent a process from terminating. Once all
foreground threads belonging to a process have terminated, the common
language runtime ends the process. Any remaining background threads
are stopped and do not complete.

Reason why my application would not exit?

We have an application written in .net, c#, winforms. We noticed that sometimes when closing the application, the process remains.
I'm not sure how I can reproduce that behavior, so I'm looking for some clues as to why the application wouldn't exit.
The application uses a bit of background threads. Thread pools. Wondering if that could be the cause. Anything else could have this effect?
If you have thread's that have IsBackground property set to false that are alive after application is closed, they will remain
The application uses a bit of
background threads. Thread pools.
Wondering if that could be the cause.
Anything else could have this effect?
It most definitely could be the cause though I cannot be certain that it actually is. One way to test this hypothesis to make sure all threads that have been explicitly created are designated as background threads. This can be done by setting Thread.IsBackground = true which will allow the application to terminate if the main thread ends. If there is at least one thread for which IsBackground = false then the CLR keeps the host process running.
Its likely to be a thread left running. If you have a look at the process in task manager you can see when a thread starts, and how many are left running when it exits by adding the 'Threads' column from the view menu.
I would start by making sure you start and end on the same thread count.
Get Process Explorer and possibly Process Monitor and see what thread is left suspended or running.
If you are able to run in debug and you close the form, VS should not return to the normal code edit mode (it will still have the pause and stop buttons from debug active). You can then press Pause and check the Threads window to see where the call stacks are stuck for threads that are still active.

Categories

Resources