6

I'm just curious about the thread and the UI Controls.

According to my test code, when I run the complied .exe file,the thread access the processbar wihout any issue. But when I debug the code, there's an InvalidOperationException. (Whatever the pooled thread or normal thread.)

So I know the thread is not allowed to access the UI Control, but why it's ok when I run the .exe file. Is it an intended design?

PS.I know we can use backgroundworker to make things elegant.

private void button1_Click(object sender, EventArgs e)
{
    Task.Factory.StartNew(dothing);//use the pooled thread
    //Thread t = new Thread(new ThreadStart(this.ThreadProcSafe));
    //t.Start();
}

private void dothing()
{
    for (int i = 1; i <= 100; i++)
    {
        progressBar2.Value = i;
        Thread.Sleep(100);
    }
}
Jim D'Angelo
  • 3,952
  • 3
  • 25
  • 39
roast_soul
  • 3,554
  • 7
  • 36
  • 73
  • It's possible that another thread is causing the exception while you're debugging. I don't pretend to know about how the debugger works on a lower level.. but it could be spawning an extra thread that is causing the race condition. – Simon Whitehead Dec 14 '12 at 03:23
  • 5
    +1 good question, easy repro. Similar Q here: http://stackoverflow.com/questions/3972727/why-is-cross-thread-operation-exception-not-thrown-while-running-exe-in-bin-debu – Jeremy Thompson Dec 14 '12 at 03:23
  • @JeremyThompson I'd say that pretty much answers this question :) – Simon Whitehead Dec 14 '12 at 03:26
  • debuging UI behavior with breakpoints is usually tricky, since when you have a breakpoint the top window becomes your visual studio, and then when you return to your UI the update/refresh method gets called but if you were not debuggin that event will no be trigger. Is best to use any kind of log to a file to detect what is going on – Mauricio Gracia Gutierrez Jul 17 '13 at 21:55

1 Answers1

-5

In my memory, you can't access UI Element in other thread but only in main thread. It's because of some multi-thread issue ( multi-access in same memory , asynchronous problem....etc.). If you want to do what you want. You can use "delegate function" in C#.

John
  • 123
  • 1
  • 8
  • 1
    I think you just told him what he said he already knows – Grimace of Despair Dec 14 '12 at 03:20
  • I know we have some ways to access the UI. But my question is that why it's ok when I run the exe file without any exception. – roast_soul Dec 14 '12 at 03:21
  • Run the example, it should `NOT` work in Run-Time. – Jeremy Thompson Dec 14 '12 at 03:22
  • It works, right ? Because your program is not complex enough. And other reason is Processbar is a simply UI element. – John Dec 14 '12 at 04:01
  • If you use Viewtable something like that which contain read/write and both your thread / system thread / main thread will interact it. Your program will crash soon. – John Dec 14 '12 at 04:06
  • And I told that, c# doesn't allow you access UI through others thread. Its design is throw you a exception with that issue. If you switch to higher level of compiler setting ( a sensitive of error, I don't know how to say that ), your Release configure will throw you same exception , too. – John Dec 14 '12 at 04:11