Tehnoon wrote:
Can you show the code in which you are invoking the thread which is creating the problem? Garbage collection will not be done unless the thread is finished with the execution. The only possibility that I am seeing here is that the system is not being able to provide all the requested resources by the program and hence the results are not coming out as expected. A look at the source code will give me a better idea of this.
Sorry for a late response. A statemachine exists in my program, and all the events are dispatched to it by seperate threads.The code is something like the following.
public class Control
{
private StateMachine myStateMachine = new StateMachine();
public void EventProc1()
{
....
EventHandler eventHandler = new EventHandler(this.Thread1);
eventHandler.BeginInvoke(this,null,null,null);
}
public void EventProc2()
{
....
EventHandler eventHandler = new EventHandler(this.Thread2);
eventHandler.BeginInvoke(this,null,null,null);
}
protected void Thread1(object sender,EventArgs e)
{
LogMessage("Start of thread1");
this.myStateMachine.Dispatch(1);
}
protected void Thread2(object sender,EventArgs e)
{
LogMessage("Start of thread2");
this.myStateMachine.Dispatch(2);
}
}
public class StateMachine
{
public void Dispatch(event id)
{
switch(event id)
{
case 1:
....
break;
case 2:
....
break;
...
default:
break;
}
}
}
Up to now, many phenomena happened, such as although the LogMessage were logged in log file, the thread just stopped somewhere afterward. Or sometimes, one thread should be waiting for an AutoResetEvent which must be set in another thread as designed, but the previous thread moves on before the AutoResetEvent is Set()... Another important supplement is that GC.Collect() is executed in another thread, ie. in EventProc1(). I have written many test programs so as to find the real reason, and still don know where a memory leak brought by no-EndInvoke will lead to the above phenomena. Looking forword to your help. Thank you!