Click to See Complete Forum and Search --> : [RESOLVED] Threads and Delegates


Pale
August 4th, 2008, 11:05 PM
Recently i have been learning how to create a multi-threaded application.

I have three items: textbox1, texbox2, and label1

In the non - UI thread i need the information from textbox1, and textbox2 for the thread to run successfully.

i plan on using a for loop to do a certain command a certain amount of times specified by my for loop ('<=10').

I also need to update label1.text on every time it goes through the for loop,
every time it goes through the for loop i need to update my int count by increments of 1 ('count++').
so 'label1.text = "Count: " + count.ToString();'

I have read a few articles on using delegates to do this but i need help "Understanding" it.

Can someone show me a quick example on how to do that?

I have created my own thread i do not use the background worker.

Thanks,

Pale.

foamy
August 5th, 2008, 01:44 AM
I made something very similar not long ago:

Non-UI class:

public delegate void CountChangedHandler();
public event CountChangedHandler CountChanged;


UI-class

private int count = 0;

//Put this in the constructor
Driver.CountChangedHandler myCountChangedHandler = Increment;
driver.CountChanged += myCountChangedHandler;

public delegate void UpdatePostCount();

private void Increment()
{
try
{
txtCount.Invoke(new UpdatePostCount(UpdateText));
}
catch (Exception x)
{
}
}

private void UpdateText()
{
try
{
count++;
label1.Text = count.ToString();
}
catch
{
}
}