Reputation: 9
I have the following code in WinForm application with one button and one label:
private void button1_Click(object sender, EventArgs e)
{
Task.Run(() => label1.Text = Thread.CurrentThread.ManagedThreadId.ToString());
}
When I started program by VS debugger, the label1.Text = ...
will throw a System.InvalidOperationException due to accessing control in working thread. That is no problem.
But if I directly run the exe, I will see the working thread id be shown on label and no exception.
What cause this difference?
update: If I start it in VS with release mode, there is no exception neither no thread id. So that here is the third result.
Upvotes: 0
Views: 64
Reputation: 1062895
Simply: in release mode it isn't managing to detect your broken code as reliably. But: the code is still just as broken either way. You should not attempt to touch UI controls from worker threads, so: don't do that! Are you sure you didn't disable Control.CheckForIllegalCrossThreadCalls
somewhere? (note: you should not disable it; I'm just asking if perhaps you have)
Upvotes: 2