When I run my windows application, a main form is loaded. I then fire another thread which keeps scanning for some RFIDs. When the RFID scanning is done I stop the process by simply setting a boolean variable to true. So that later I can resume it when needed.
Soemthing like
while(!StopThread)
{
.... my thread code
}
When someone clicks Alt + F4 on the main window, the form closes down. But I still have a process running in my task manager. This has to do with the thread that I fired because if I disable it, the application shuts down clean.
Here is what I tried and did not work.
1. I set the boolean variable to true so that the thread function stopes. I then tried calling m_workerThread.Abort() and handled a excetion for ThreadAbortException. Then in the finally block I put m_workerThread.Join(). I also tried removing the join.
2. In the dispose of the main form, I tried to do a getprocessbyname and kill that process if its running. That did not work either.
Can someone please tell me why would this be happening and whats the clean way to solve this problem ?
Adwait
Loading
AlanPosted Aug 15, 2007, 7:31 PM
The usual objection to using Environment.Exit in a Windows Forms application is that it's more abrupt than Application.Exit and the latter should therefore be preferred for a more orderly shutdown.
However, as you've already cleaned up, I don't think that Enviroment.Exit will do any harm here and it may be the only way you can force the entire process to terminate as Application.Exit doesn't appear to stop non-UI threads.
AdwaitPosted Aug 15, 2007, 6:54 PM
Adwait
AlanPosted Aug 15, 2007, 6:09 PM
AdwaitPosted Aug 15, 2007, 5:07 PM
AlanPosted Aug 15, 2007, 5:02 PM
It might be better to prevent the user from closing the main form until RFID scanning is completed. You could do this by handling the main form's FormClosing event. For example:
private void frmMain_FormClosing(Object sender, FormClosingEventArgs e)
{
if (ScanningThread.IsAlive)
{
e.Cancel = true; // cancel form closure
MessageBox.Show("Unable to close form until RFID scanning completed");
}
}