473,569 Members | 2,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Refre sh);

// Create a timer that waits one second, then invokes every 10 seconds.
timer = new Timer(timerDele gate,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 fRefreshDelegat e();
Then we also have the Refresh method that is called from the timerthread.

public void Refresh(object state)
{
if (! this.InvokeRequ ired )
{
base.Refresh();
}
else
{
fRefreshDelegat e fDelRefresh = new fRefreshDelegat e(this.Refresh) ;
this.Invoke(fDe lRefresh);
}
}

public override void Refresh()
{
if (! this.InvokeRequ ired )
{
base.Refresh();
}
else
{
fRefreshDelegat e fDelRefresh = new fRefreshDelegat e(this.Refresh) ;
this.Invoke(fDe lRefresh);
}
}


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.InvokeReq uired' 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 3906
Serge <se**********@h otmail.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.co m>
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
3477
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 running on a server, there is nobody there to answer the dialog, so I have another thread that calls Abort() on the first thread in the event that the...
2
10514
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 threadTerminatedGood = uploadThread.Join(client.transmissionTimeout*60*1000); if (!threadTerminatedGood) { throw new Exception("The upload thread was interrupted...
1
3597
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 only if I'm in the debugger. Here's a stack trace with non-user code shown -- the call to RegisterNotifySource, at the top of the stack, never...
1
1929
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: --- my_thread.py --- import threading from subprocess import Popen, PIPE, STDOUT
51
54795
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() # This thread is expected to finish within a second
3
7508
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 Sub to the worker so that the latter can refresh the DataGrid after updating the DataTable driving it. When I run the app under the debugger,...
4
2374
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 just hangs, nothing being written it just hangs. when I comment out the "Invoke(new UpdateTextCallback(UpdateText));" it works fine. What am I...
9
6162
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 method exposed by the server using the proxy from a separate thread, all is fine. However, if I do the same from the main thread, the call hangs and...
1
3292
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 gets a problem and the way this is handled on AS400 is by hanging. If I was to connect directly this would mean my main process would hang as well, so...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.