Correct way to call a Delegate?

P

pz0071

G'day,

I think I got my head around using delegates, particularily when
updating the user interface from another thread. I got it all sussed,
just not 100% sure on the correct syntax for calling the delegate.

Is
this.Invoke(delUpdateUI, args);
Same as
delUpdateUI(i);

The following code is a win form. I press button and it starts a new
worker thread that counts from 1 to 20,000 and updates a label as it
goes.

Can someone tell which of the two methods calc() or calc2() has the
correct way of calling the delUpdateUI() delegate?

Do both delegate calls get marshalled to the UI thread, just different
way of expressing the same thing? Both work just fine, so which is the
correct way?

Cheers,
PeterZ


------------------------------------------------------
private void btnStartCount_Click(object sender, System.EventArgs e)
{
// Run the calculation on a sperate worker thread.
lblCounter.Text = "";
Thread workerThread = new Thread(new ThreadStart(workerSub));
workerThread.Start();
}

private void workerSub()
{
// "calc" and "calc2" do the same thing, just different way of
// calling the delUpdateUI() delegate. Which is the correct of
// the two?

//calc(20000));
calc2(20000);
MessageBox.Show("I'm finished!");
}

public void calc(int countTo)
{
int i;

// Instantiate the delegate reponsible for updating the UI.
DelUpdateUI delUpdateUI = new DelUpdateUI(updateUI);

for (i=1; i <=countTo; i++)
{
// The caller is on a different thread to the UI, must
// update the label through delegate.
object[] args = new object[] {i};
this.Invoke(delUpdateUI, args);
}
}

public void calc2(int countTo)
{
int i;

// Instantiate the delegate reponsible for updating the UI.
DelUpdateUI delUpdateUI = new DelUpdateUI(updateUI);

for (i=1; i <=countTo; i++)
{
// The caller is on a different thread to the UI, must
// update the label through delegate.
delUpdateUI(i);
}
}

public delegate void DelUpdateUI(int count);
private void updateUI(int count)
{
// Update the user interface.
lblCounter.Text = count.ToString();
}

------------------------------------------------------
 
B

Bruce Wood

OK... I'm going out on a limb here. See if I get clobbered for it. :)

The correct way to call back into the UI thread is by using Invoke().
The difference between doing this and calling the delegate directly are
as follows.

If you call the delegate directly, the delegate's method code will run
under the control of the calling thread. This means (in your case) that
the label in the UI will be updated under the control of your worker
thread, which is a no-no. Only the UI thread should update UI controls.

Using Invoke causes the delegate's method code to run under the control
of the main (UI) thread.
 
J

Jon Skeet [C# MVP]

I think I got my head around using delegates, particularily when
updating the user interface from another thread. I got it all sussed,
just not 100% sure on the correct syntax for calling the delegate.

Is
this.Invoke(delUpdateUI, args);
Same as
delUpdateUI(i);

No.

delUpdateUI.Invoke(args)

is the same as

delUpdateUI(i).

When you call Invoke on a Control, that makes sure that the delegate is
called on the UI thread. A straight delegate call doesn't.

calc() has it correctly; calc2() doesn't.

Note that you might want to use BeginInvoke - you don't really need to
wait for the UI to be updated before you proceed, do you? Having said
which, chances are you'll only see 20,000 if you call BeginInvoke,
because it'll all happen *very* quickly.
 
J

Jon Skeet [C# MVP]

Bruce Wood said:
OK... I'm going out on a limb here. See if I get clobbered for it. :)

The correct way to call back into the UI thread is by using Invoke().
The difference between doing this and calling the delegate directly are
as follows.

If you call the delegate directly, the delegate's method code will run
under the control of the calling thread. This means (in your case) that
the label in the UI will be updated under the control of your worker
thread, which is a no-no. Only the UI thread should update UI controls.

Using Invoke causes the delegate's method code to run under the control
of the main (UI) thread.

No need for clobbering at all - that's exactly correct. (Some would
argue that a better way of calling into the UI thread is to use
BeginInvoke, but it depends on the situation.)
 
P

pz0071

Thanks Bruce and Jon - exactly the respnses I was looking for!

I can now safely tackle the weird world of multithreading!
:)

Cheers,
PeterZ
 

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