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();
}
------------------------------------------------------
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();
}
------------------------------------------------------