By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,473 Members | 3,292 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,473 IT Pros & Developers. It's quick & easy.

thread hangs .. why ?

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Serge <se**********@hotmail.com> wrote:

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.