MultiThreading and Notification

G

Guest

I've been banging my head over this for a few hours and I'm not finding any
good code samples to demonstrate MultiThreading in C# Framework 1.1 that
shows me how to basically have the UI code stay separate from the Threading
code but allow a return parameter to the UI.

I'd like to have something like the following pseudocode....Is this even
possible?

main.cs
{
Do some work
Setup some threads
launch thread a
launch thread b
UI is able to do some more work
get notified when thread a AND thread b has been completed
do some conditional work only when tr
}

class longrunningstuff.cs
{
do some really long running stuff (thread a and b, etc)
}
 
N

Nicholas Paldino [.NET/C# MVP]

Zachary,

Have you looked into the Invoke (or BeginInvoke/EndInvoke) method on the
Control class? All you have to do is make the Control instance from the UI
(your form, a control, something) available to the thread procedure, and
then call Invoke. You would pass a delegate for the method to be called on
the UI thread, as well as the parameters.

Hope this helps.
 
P

Pete Davis

There are a number of synchronization objects availble, Mutex,
AutoResetEvent, ManualResetEvent, etc.These work great if you want to wait
for an event to take place.

It sounds like what you want is a simple delegate, however. In this case,
you're not waiting for the event, but the event is getting triggered.
Basically, here's how it would work.

You create a delegate (or add a regular .NET event, which is just a delegate
anyway) in your UI thread which you'll pass to the thread. Pass it using
whatever mechanism you're using to pass data to the threads. The threads
will then call the delegate whenever the event has taken place that you're
waiting for (such as them completing their work)

What you'll probably need to do, however, is use Invoke or BeginInvoke
instead of calling the delegate directly. This is because you can't update
the UI from a separate thread and doing anything to your UI via a direct
call from the separate threads will throw an exception.

There are plenty of examples out there for doing this kind of stuff. A
simple google search of: C# threading returns 519,000 results. Many on the
first 2 pages are at least partially applicable to your situation.

Pete
 
G

Guest

Hi Zachary,
if I understood you correctly you want your main UI thread to do some work
then wait for 2 threads to finish their work, then let the main UI thread
continue. To accomplish this you can use a WaitHandle and some
ManualResetEvents. For example:

using System;
using System.Threading;

namespace ConsoleApplication11
{
class Program
{
private static ManualResetEvent _thread1Wait;
private static ManualResetEvent _thread2Wait;

static void Main(string[] args)
{
Console.WriteLine("UI DOING WORK...");

_thread1Wait = new ManualResetEvent(false);
_thread2Wait = new ManualResetEvent(false);

Thread t1 = new Thread(new ThreadStart(Thread1Work));
Thread t2 = new Thread(new ThreadStart(Thread2Work));

t1.Start();
t2.Start();

Console.WriteLine("UI DOING MORE WORK...");

//wait for both threads to finish
WaitHandle.WaitAll(new WaitHandle[] { _thread1Wait, _thread2Wait
});

Console.WriteLine("ALL DONE");
Console.ReadLine();
}

private static void Thread1Work()
{
System.Threading.Thread.Sleep(5000);

Console.WriteLine("Thread1 complete");

//indicate thread1 has finished to
//main UI thread
_thread1Wait.Set();
}

private static void Thread2Work()
{
System.Threading.Thread.Sleep(10000);

Console.WriteLine("Thread2 complete");

//indicate thread2 has completed to
//main UI thread
_thread2Wait.Set();
}
}
}


Hope that helps
Mark R Dawson
http://www.markdawson.org
 

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