I have this application "x" written in C# that has 1 thread in it and uses Form1_Close as the closing event. I have this other application that I use to kill a process and it is written in Embedded Visual C++. The way it kills a process is by sending a processes windows the PostMessage(WM_CLOSE).
Anyways I am in the middle of converting my application from .NET1.1 to .NET2.0 and now I can't use kill to shut it down properly. My application does indeed receive the WM_CLOSE but the other running thread is being blocked by the GUI Thread when it tries to execute a invoke on it. This only happens in .NET2.0 it seems because it works fine in .NET1.1. Below is some logic of what I am doing. Basically when I do Invoke it never gets to the CloseThread Method. I have tried BeginInvoke, Invoke, Delegates, etc. If I put a timer and have the thread get out of the while loop and close the form1, it works fine. The only issue is when my Kill program does a PostMessage(WM_CLOSE) on the window is where I am having the problem. Again, this code works perfect in .NET1.1 but it doesn't work in .NET2.0. Does anyone have any insight on my issue?
Anyways I am in the middle of converting my application from .NET1.1 to .NET2.0 and now I can't use kill to shut it down properly. My application does indeed receive the WM_CLOSE but the other running thread is being blocked by the GUI Thread when it tries to execute a invoke on it. This only happens in .NET2.0 it seems because it works fine in .NET1.1. Below is some logic of what I am doing. Basically when I do Invoke it never gets to the CloseThread Method. I have tried BeginInvoke, Invoke, Delegates, etc. If I put a timer and have the thread get out of the while loop and close the form1, it works fine. The only issue is when my Kill program does a PostMessage(WM_CLOSE) on the window is where I am having the problem. Again, this code works perfect in .NET1.1 but it doesn't work in .NET2.0. Does anyone have any insight on my issue?
Code:
public EventHandler ThreadCloseProc;
public int giThreadCount = 0;
public bool gbCloseRequested = false;
Form1()
{
Thead start = new Thread(myProcess);
start.Start();
ThreadCloseProc = new EventHandler(CloseThread);
}
private void CloseThead(Object o, EventArgs e)
{
iThreadCount--;
if(iThreadCount == 0)
this.Close();
}
private void myProcess()
{
do
{
Thread.Sleep(1000);
}
while(!gbCloseRequested);
this.BeginInvoke(ThreadCloseProc);
}
private void Form1_Closing(Object o, CanelEvent e)
{
gbCloseRequested = true;
if(giThreadCount >0)
{
e.cancel = true;
}
else
{
e.cancel = false;
}
}