private void Thread1_Exe()
{
try
{
int sleepValT1 = Convert.ToInt32(listBoxT2.SelectedValue);
int StartLoop = 0;
int EndLoop = 10000;
for (int i = StartLoop; i <= EndLoop; i++)
{
Dispatcher.BeginInvoke(
new Action(() => listboxE1.Items.Add("T1: Execution Count> " + i.ToString())));
Thread.Sleep(sleepValT1);
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
I was trying to call the above function on different thread
private void thread1_Click(object sender, RoutedEventArgs e)
{
threadBtn1.IsEnabled = false;
Thread t1 = new Thread(new ThreadStart(Thread1_Exe));
t1.Start();
}
But the Exception occurs when i select the value from the listbox and try to save it in the variable and then try to pass that value in
Thread.Sleep()
I always get this Exception that the different thread owns this object. Tried Many things. Please help me out where i am doing mistake.
Thanks
Any access to a UI element, no matter if it is read or write, must be called through main UI thread. This thread is accessible through the
Dispatcher
.In your code I see a line where this is not the case: when you are calling
listBoxT2.SelectedValue
To fix the error call this code on the
Dispatcher
Thread and save it to a local variable:The rest of the code should work fine.