473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

thread hangs .. why ?

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
1 3857
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Andy Fish | last post by:
Hi, I am using COM interop to invoke MS word from my .net app. If word pops up a dialog box, my thread obviously hangs until the dialog is answered. Here's the problem: because the app is...
2
by: Asad Khan | last post by:
I call the following method from my main form method: uploadThread = new Thread(new ThreadStart (this.doFTPUpload)); uploadThread.Name = "FTP Upload"; uploadThread.Start(); bool...
1
by: Gregory Hassett | last post by:
Hi, I have a web service called GetComputerNameAndDescription which I can call from the main thread of my app. If I call it from a separate thread, however, it hangs on the call to Invoke() -- but...
1
by: Toon Verstraelen | last post by:
Hi, I recently had a thread problem and I could reduce it to a very short example that shows the problem. I hope it has its origin in my misunderstanding of how python threads work. Here it is:...
51
by: Hans | last post by:
Hi all, Is there a way that the program that created and started a thread also stops it. (My usage is a time-out). E.g. thread = threading.Thread(target=Loop.testLoop) thread.start() ...
3
by: David Cartwright | last post by:
Hi all, I'm having a weird time with a call to the Refresh() method of a DataGridView. I have a VB.NET 2005 Windows application with a main form and a "worker" thread. The main form delegates a...
4
by: RSH | last post by:
Hi, I have an Asynchronus process that is reporting back to the UI at regular intervals. I read several posts on how to use delegates to make this happen. But for some reason my application...
9
by: =?iso-8859-1?Q?Jos=E9_Joye?= | last post by:
I'm currently running some tests with WCF in In-proc environment. I have created a small winform application that starts the host in the main thread with an named pipe endpoint. If I call a...
1
by: Chrace | last post by:
Hi all, I have a problem with with Thread.Join( Timeout ) where the timeout never occurs. I basically need to make a connection to an AS400 box which works fine. Once in a blue moon the AS400...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.