For Programmers: Free Programming Magazines  


Home > Archive > C# > April 2006 > Cross Thread Calls - is it ok to use Event to notify changes?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Cross Thread Calls - is it ok to use Event to notify changes?
Evert Wiesenekker

2006-04-11, 7:02 pm

I read some postings on how to make Cross Thread Calls and now I get a
little bit .

My situation:

I have a Windows Forms application. Inside this application I start a
Thread, which starts method X (inside class Z) which contains lets say
3 different steps (call it a workflow). Now I want my to display in my
Forms applications which step thread X is processing.

My idea is to raise inside method X for every step a custom event.
Inside my form I have an Eventhandler which displays the status.

Now when reading postings I only found examples which use an
'InvokeRequired' construction to update an Form controls or using the
MethodInvoke construction. Now what I do not like is a tight coupling
of the 'Thread-method' and the GUI. I prefer the event mechanism
described above. But now I get the eerie feeling this is not safe to
use.

Am I right or wrong?

Yours sincerely,

Evert Wiesenekker

Linda.Konthaar@gmail.com

2006-04-13, 4:06 am

Hi Evert,

i think it is safe.


Greetings,
Linda Konthaar

gumson

2006-04-13, 8:01 am

Hi

I use the InvokeRequired pattern alot. It works very well.

Example:

private delegate void DoWorkHandler();

class MyForm : Form
{
// Method called from another thread than the GUI
DoWork()
{
if (this.InvokeRequired)
{
Invoke(new DoWorkHandler(this.DoWork), new object[] {}); //
here you put parameters needed for the DoWork method
}
else
{
// Perform gui thread actions
}
}
}

If you do not want to use this pattern you'll have to set the static
variable Control.CheckForIllegalCrossThreadCalls to false and handle
the method synchronization with an event sync object or a monitor
object.

Thanks
Gumson

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com