none
Cross-thread operation not valid:??

    Question

  • Hi,
    I want to call a method asychonously.That method is updating UI of my windows form.
    I am getting exception "Cross-thread operation not valid:".

    Any solution!!



    Thanks in advance.
    Tuesday, June 03, 2008 11:53 AM

Answers

  • You cannot do that.
    The UI thread and the asynchronous thread need to be different.
    Any other thread other than the UI thread cannot update the UI. Only the UI thread can do it.
    I suggest you read up on this before implementing threading.
    Here is one such article.

    Hope this helps !!
    Always mark the post that helped you as "Answer". That helps future users to locate answers quickly.
    • Proposed as answer by Somnath Paul Tuesday, June 03, 2008 12:18 PM
    • Marked as answer by jack 321 Friday, June 06, 2008 2:37 AM
    Tuesday, June 03, 2008 12:18 PM
  • Hi sandy_vce,

    As Paul said above you need to call the correct thread.  Personally I find the easiest way to do this is to use a background worker and delegate.  Please see the example below:  (it assumes you are working with a windows form. btw) 

    1 private void MethodOnThreadOne() {  
    2     // Background Worker Starts A New Thread  
    3     BackgroundWorker bw = new BackgroundWorker();  
    4     // Attach to the DoWork event (Note: you can also handle the RunWorkerCompleted event to see when the thread finishes.)  
    5     bw.DoWork += new DoWorkEventHandler(bw_DoWork);  
    6     // Start the Background Worker - this triggers the DoWork event and calls the handler below...  
    7     bw.RunWorkerAsync();    
    8 }  
    9  
    10 //This is Thread2  
    11 private void bw_DoWork(object sender, DoWorkEventArgs e) {  
    12     //Now that the method above has finished executing your UI will not crash etc.. and you do processing on thread 2.  
    13  
    14     //In the event you need to do something on thread one create a method call  
    15     doOperationOnThreadOne();  
    16 }  
    17  
    18 private delegate void dNoParamsDelegate();  
    19  
    20 private void doOperationOnThreadOne() {  
    21     // Check whether you are on the correct thread  
    22     if(this.InvokeRequired) {  
    23         // Not the correct thread, create a delegate to call this method  
    24         dNoParamsDelegate d = new dNoParamsDelegate(doOperationOnThreadOne);  
    25         // Invoke the delegate  
    26         this.Invoke(d);  
    27     } else {  
    28         // This is the correct thread!  Place your code here.  
    29         // e.g. Update a text box - adjust the value of a progress bar etc..  
    30     }  
    31

    Hope this helps.. use the link in post above to find out more!

    James
    • Proposed as answer by Irish Newton Tuesday, June 03, 2008 1:24 PM
    • Marked as answer by Hanzheng Zou Thursday, June 05, 2008 6:24 AM
    • Unmarked as answer by Hanzheng Zou Thursday, June 05, 2008 6:28 AM
    • Marked as answer by jack 321 Friday, June 06, 2008 2:37 AM
    Tuesday, June 03, 2008 1:24 PM
  • Read the thread: 

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3169053&SiteID=1

    For the similar situation.

    Wishes.
    • Marked as answer by Hanzheng Zou Monday, June 09, 2008 5:42 AM
    Thursday, June 05, 2008 6:32 AM

All replies

  • You cannot do that.
    The UI thread and the asynchronous thread need to be different.
    Any other thread other than the UI thread cannot update the UI. Only the UI thread can do it.
    I suggest you read up on this before implementing threading.
    Here is one such article.

    Hope this helps !!
    Always mark the post that helped you as "Answer". That helps future users to locate answers quickly.
    • Proposed as answer by Somnath Paul Tuesday, June 03, 2008 12:18 PM
    • Marked as answer by jack 321 Friday, June 06, 2008 2:37 AM
    Tuesday, June 03, 2008 12:18 PM
  • Hi sandy_vce,

    As Paul said above you need to call the correct thread.  Personally I find the easiest way to do this is to use a background worker and delegate.  Please see the example below:  (it assumes you are working with a windows form. btw) 

    1 private void MethodOnThreadOne() {  
    2     // Background Worker Starts A New Thread  
    3     BackgroundWorker bw = new BackgroundWorker();  
    4     // Attach to the DoWork event (Note: you can also handle the RunWorkerCompleted event to see when the thread finishes.)  
    5     bw.DoWork += new DoWorkEventHandler(bw_DoWork);  
    6     // Start the Background Worker - this triggers the DoWork event and calls the handler below...  
    7     bw.RunWorkerAsync();    
    8 }  
    9  
    10 //This is Thread2  
    11 private void bw_DoWork(object sender, DoWorkEventArgs e) {  
    12     //Now that the method above has finished executing your UI will not crash etc.. and you do processing on thread 2.  
    13  
    14     //In the event you need to do something on thread one create a method call  
    15     doOperationOnThreadOne();  
    16 }  
    17  
    18 private delegate void dNoParamsDelegate();  
    19  
    20 private void doOperationOnThreadOne() {  
    21     // Check whether you are on the correct thread  
    22     if(this.InvokeRequired) {  
    23         // Not the correct thread, create a delegate to call this method  
    24         dNoParamsDelegate d = new dNoParamsDelegate(doOperationOnThreadOne);  
    25         // Invoke the delegate  
    26         this.Invoke(d);  
    27     } else {  
    28         // This is the correct thread!  Place your code here.  
    29         // e.g. Update a text box - adjust the value of a progress bar etc..  
    30     }  
    31

    Hope this helps.. use the link in post above to find out more!

    James
    • Proposed as answer by Irish Newton Tuesday, June 03, 2008 1:24 PM
    • Marked as answer by Hanzheng Zou Thursday, June 05, 2008 6:24 AM
    • Unmarked as answer by Hanzheng Zou Thursday, June 05, 2008 6:28 AM
    • Marked as answer by jack 321 Friday, June 06, 2008 2:37 AM
    Tuesday, June 03, 2008 1:24 PM
  • Google for "InvokeRequired". That should give you some pointers.
    Tuesday, June 03, 2008 1:24 PM
  • Read the thread: 

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3169053&SiteID=1

    For the similar situation.

    Wishes.
    • Marked as answer by Hanzheng Zou Monday, June 09, 2008 5:42 AM
    Thursday, June 05, 2008 6:32 AM