Communicating between C# threads
Hi,
can someone inform me what the standard method is for
communicating between threads in C#, eg to perform synchronisation, some example code would be appreciated.
Thanks
Paul
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
YousefPosted Apr 15, 2007, 6:07 AM
example:
volatile bool bSayHello;
void MainThread()
{
// This code executes on the Main thread
while(true)
{
if(bSayHello)
{
Console.WriteLine("Hello World!");
bSayHello=false;
}
Thread.Sleep(1000);
}
}
void WorkerThread()
{
// This code executes on the worker thread
while(true)
{
// Request the main thread to Say Hello every 5 seconds.
Thread.Sleep(5000);
bSayHello=true;
}
}
The worker thread will request the main thread to type "Hello World" every 5 seconds.
Hope this helps