Hii
I want to work with MultyThreading concept and I am using the following code.
But when i try to run the the MultiThreading (multiThreadedButton_Click()) its giving an error at
FillList() function.
The code is :
private void Delay(int v_MilliSeconds)
{
long startTime = Environment.TickCount;
while( Environment.TickCount - startTime < v_MilliSeconds )
{
}
}
/*
* Fill the listbox with hello world buttons. Pause for a period
* of time between each message.
*/
private void FillList()
{
responseList.Items.Clear();
responseCountLabel.Text = "0";
ToggleButtons(false);
for (int i = 0; i < 10; i++)
{
responseList.Items.Add("Thread #" + i);
this.Delay(1000);
}
this.ToggleButtons(true);
}
/*
* Toggle the form buttons on or off accordingly.
*/
private void ToggleButtons(bool v_Toggle)
{
this.nonThreadedButton.Enabled = v_Toggle;
this.multiThreadedButton.Enabled = v_Toggle;
}
private void testResponseButton_Click(object sender, EventArgs e)
{
this.responseCountLabel.Text =
Convert.ToString(Convert.ToInt32(this.responseCountLabel.Text) + 1);
}
private void multiThreadedButton_Click(object sender, EventArgs e)
{
// Launch a thread to do the update
System.Threading.Thread sampleThread =
new System.Threading.Thread(
new System.Threading.ThreadStart(this.FillList));
sampleThread.Start();
}
The error is :
Cross-thread operation not valid: Control 'responseCountLabel' accessed from a thread other than the thread it was created on.
Please help me..
Thanks in Advance.