thread hangs .. why ?

S

Serge

Hi,

I am having some lock up problems in my c# code.
when from a 2nd thread (worker thread) i call a normal standard function
like Trace.WriteLine the 2nd thread freezes/hangs.
The 2nd thread is started with a normal threadstart, etc.

When looking on the internet I found some articles that hangs can be caused
by unmanaged threads but I don't think I am doing this (am not using coms,
.... )


Enclosed is a similar problem I found during some tests to analyze this
problem.

From a class that is running in the first/main thread I create a form
_frmStatus.
This form should show the progress from other threads like my worker thread.
The worker thread is running in a seperate thread because it is performing
cpu intensive and time consuming calculations.
In the main thread for the test I create a timer thread that will call a
Refresh method inside the status form called _frmStatus.


// Create the delegate that invokes methods for the timer.
timerDelegate = new TimerCallback(_frmStatus.Refresh);

// Create a timer that waits one second, then invokes every 10 seconds.
timer = new Timer(timerDelegate,null,1000, 10000);



Then in the form the following code exists:

//first we define a delegate that we need for the refresh
private delegate void fRefreshDelegate();


Then we also have the Refresh method that is called from the timerthread.



public void Refresh(object state)
{
if (! this.InvokeRequired )
{
base.Refresh();
}
else
{
fRefreshDelegate fDelRefresh = new fRefreshDelegate(this.Refresh);
this.Invoke(fDelRefresh);
}
}

public override void Refresh()
{
if (! this.InvokeRequired )
{
base.Refresh();
}
else
{
fRefreshDelegate fDelRefresh = new fRefreshDelegate(this.Refresh);
this.Invoke(fDelRefresh);
}
}




My purpose is that every code can call the refresh method (from each thread)
and that the form will properly handle the code.
if it is from a different thread it will invoke the method.

What now happens is that the TimerThread properly calls the
public void Refresh(object state)

Then the 'this.InvokeRequired' detects it is from a different thread and the
code continues upto the this.Invoke ... line.

then the line this.invoke is executed ... and ... nothing else happens. The
thread continues to exist and the 'public override void Refresh()' is never
called.

Can someone tell me why this thread hangs? Or why even a call to
Trace.WriteLine makes the worker thread hang.
i would expect the Trace.WriteLine to be a threadsafe operation.

Thanks VERY much in advance for your help,

Regards, Serge
 
J

Jon Skeet [C# MVP]

Can someone tell me why this thread hangs? Or why even a call to
Trace.WriteLine makes the worker thread hang.
i would expect the Trace.WriteLine to be a threadsafe operation.

It's hard to work out what's going on without a *complete* program to
run/test/modify. Could you post a short but complete program which
demonstrates the problem?
See http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top