473,395 Members | 1,468 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,395 software developers and data experts.

non blocking wait in C#

I have thread t1 . It spawns thread t2. I want to wait in thread t1
until the execution of thread t2 in completed. Bu t I do not want it to
be a blocking wait since I want thread t1 to be responsive to WM_PAINT
messages. I know how to do it in VC++ , but I have no idea how it can be
done in C#. Please help!!!

Regards,
Dave

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
7 12118
Hi Dave,

You might signalize from t2 to t1 in some way, like calling a method or
raising an ManualEvent perhaps.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Dave Hardy" <dv******@yahoo.com> wrote in message
news:OX**************@TK2MSFTNGP10.phx.gbl...
I have thread t1 . It spawns thread t2. I want to wait in thread t1
until the execution of thread t2 in completed. Bu t I do not want it to
be a blocking wait since I want thread t1 to be responsive to WM_PAINT
messages. I know how to do it in VC++ , but I have no idea how it can be
done in C#. Please help!!!

Regards,
Dave

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
Instead of explicitly spawining a thread, wrap your thread's entry point
method in a delegate and call BeginInvoke(), passing another callback
delegate which is to be called when your task completes, e.g.

public void StartIt()
{
new MyDelegate(threadStart).BeginInvoke(new AsynchCallback(myCallback),
null);
}

private void threadStart()
{
// Do your work in here
}

private void myCallback(IAsynchResult ar)
{
// To update the UI, marshal the call to the UI thread
this.Invoke(new MethodInvoker(updateUI));
}

private void updateUI()
{
this.someTextBox.Text = "Thread is done!";
}

The main thing to remember is that your callback function will be invoked on
the same thread as performed the long processing work. So, if you want to
update your UI, you'll need to use the form's Invoke method to marshal the
call to the UI thread.

Hope that helps -
Ken
"Dave Hardy" <dv******@yahoo.com> wrote in message
news:OX**************@TK2MSFTNGP10.phx.gbl...
I have thread t1 . It spawns thread t2. I want to wait in thread t1
until the execution of thread t2 in completed. Bu t I do not want it to
be a blocking wait since I want thread t1 to be responsive to WM_PAINT
messages. I know how to do it in VC++ , but I have no idea how it can be
done in C#. Please help!!!

Regards,
Dave

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
Whoops -- left out the call to EndInvoke() within the myCallback() function.
You should call EndInvoke() on the delegate to ensure .NET cleans up
properly after it, e.g.

private void myCallback(IAsynchResult ar)
{
MyDelegate dlg = (MyDelegate) ((AsynchResult)ar).AsynchDelegate;
dlg.EndInvoke(ar);

// To update the UI, marshal the call to the UI thread
this.Invoke(new MethodInvoker(updateUI));
}
Ken
"Ken Kolda" <ke*******@elliemae-nospamplease.com> wrote in message
news:uC**************@TK2MSFTNGP10.phx.gbl...
Instead of explicitly spawining a thread, wrap your thread's entry point
method in a delegate and call BeginInvoke(), passing another callback
delegate which is to be called when your task completes, e.g.

public void StartIt()
{
new MyDelegate(threadStart).BeginInvoke(new AsynchCallback(myCallback), null);
}

private void threadStart()
{
// Do your work in here
}

private void myCallback(IAsynchResult ar)
{
// To update the UI, marshal the call to the UI thread
this.Invoke(new MethodInvoker(updateUI));
}

private void updateUI()
{
this.someTextBox.Text = "Thread is done!";
}

The main thing to remember is that your callback function will be invoked on the same thread as performed the long processing work. So, if you want to
update your UI, you'll need to use the form's Invoke method to marshal the
call to the UI thread.

Hope that helps -
Ken
"Dave Hardy" <dv******@yahoo.com> wrote in message
news:OX**************@TK2MSFTNGP10.phx.gbl...
I have thread t1 . It spawns thread t2. I want to wait in thread t1
until the execution of thread t2 in completed. Bu t I do not want it to
be a blocking wait since I want thread t1 to be responsive to WM_PAINT
messages. I know how to do it in VC++ , but I have no idea how it can be
done in C#. Please help!!!

Regards,
Dave

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 16 '05 #4
Hi Ken...
Thanks for the suggestion. But, if the callback is being called in
the context of thread t2, it does not solve my problem.t2 is my worker
thread.t1 is a UI thread. After completion of the processing in t2, I
want to display some UI in t1. I do not want to display UI in callback
if it is being invoked in the context of t2. I want to keep all the UI
in thread t1.

Any other suggestion?

Regards,
Dave

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
private void StartWorkigThread
{
//this is execute from thread 1
System.Threading.ThreadPool.QueueUserWorkItem(new
System.Threading.WaitCallback(this.StartWorkingThr ead));

//thread 1 is now free to execute all the event like repait or button
pushing .... or execute others function

}

private void StartWorkingThread(object state)
{
//this is exectute from thread 2
.........

this.Invoke(new System.Threading.ThreadStart(this.WorkingThreadIsD one));
//thread 2 go back in thread pool
}

private void WorkingThreadIsDone()
{
//this is execute from thread 1
....
}

"Dave Hardy" <dv******@yahoo.com> schrieb im Newsbeitrag
news:OX**************@TK2MSFTNGP10.phx.gbl...
I have thread t1 . It spawns thread t2. I want to wait in thread t1
until the execution of thread t2 in completed. Bu t I do not want it to
be a blocking wait since I want thread t1 to be responsive to WM_PAINT
messages. I know how to do it in VC++ , but I have no idea how it can be
done in C#. Please help!!!

Regards,
Dave

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #6
Absolutely, you need to keep all of the UI code in thread t1. This is why,
in your callback, you use the form's Invoke() method. This method allows you
to invoke a method on the UI thread instead of the thread on which the
callback occurs. So, in the code:

private void myCallback(IAsynchResult ar)
{
// To update the UI, marshal the call to the UI thread
this.Invoke(new MethodInvoker(updateUI));
}

private void updateUI()
{
this.someTextBox.Text = "Thread is done!";
}

The function myCallback() is running on thread t2, but by calling
this.Invoke(...), the function updateUI will run on thread t1.

Ken
"Dave Hardy" <dv******@yahoo.com> wrote in message
news:u9**************@TK2MSFTNGP11.phx.gbl...
Hi Ken...
Thanks for the suggestion. But, if the callback is being called in
the context of thread t2, it does not solve my problem.t2 is my worker
thread.t1 is a UI thread. After completion of the processing in t2, I
want to display some UI in t1. I do not want to display UI in callback
if it is being invoked in the context of t2. I want to keep all the UI
in thread t1.

Any other suggestion?

Regards,
Dave

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #7
Dave Hardy <dv******@yahoo.com> wrote:
I have thread t1 . It spawns thread t2. I want to wait in thread t1
until the execution of thread t2 in completed. Bu t I do not want it to
be a blocking wait since I want thread t1 to be responsive to WM_PAINT
messages. I know how to do it in VC++ , but I have no idea how it can be
done in C#. Please help!!!


It sounds to me like you shouldn't really be waiting in t1 at all - you
should instead be subscribing to an event in the calculation of t2 to
let you know when the execution has completed, using Control.Invoke to
"get back" to t1.

See http://www.pobox.com/~skeet/csharp/threads

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David Sworder | last post by:
This message was already cross-posted to C# and ADO.NET, but I forgot to post to this "general" group... sorry about that. It just occured to me after my first post that the "general" group readers...
9
by: Koo | last post by:
How do you create your own blocking function? Koo
23
by: David McCulloch | last post by:
QUESTION-1: How can I detect if Norton Internet Security is blocking pop-ups? QUESTION-2a: How could I know if a particular JavaScript function has been declared? QUESTION-2b: How could I...
7
by: Michi Henning | last post by:
Hi, I'm using a non-blocking connect to connect to a server. Works fine -- the server gets and accepts the connection. However, once the connection is established, I cannot retrieve either the...
11
by: Michi Henning | last post by:
Hi, I'm using a blocking Select() call on a socket with a timeout value of -1. I'd expect the call to block indefinitely, but it doesn't. When I use Poll() instead, a timeout of -1 works fine...
6
by: roger beniot | last post by:
I have a program that launches multiple threads with a ThreadStart method like the following (using System.Net.Sockets.Socket for UDP packet transfers to a server): ThreadStart pseudo code: ...
20
by: Charles Law | last post by:
Consider the following scenario: A data packet is sent out of a serial port and a return packet is expected a short time later. The application sending the packet needs to send another packet...
3
by: loosecannon_1 | last post by:
I get a 90-120 second blocking when send 15 or so simultaneous queries to SQL Server 2000 that query a view made up of two joined tables. After each query is blocking for the same amount of time...
5
by: eyalc1978 | last post by:
Hi Does someone knows if fopen() is a non-blocking function in a sense that if there isn't sufficient disk space to open a file in write mode, then the function will not wait but fail? I find...
5
by: Thomas Christensen | last post by:
This issue has been raised a couple of times I am sure. But I have yet to find a satisfying answer. I am reading from a subprocess and this subprocess sometimes hang, in which case a call to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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...
0
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...

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.