0

I am getting an exception "System.InvalidOperationException: Cross-thread operation not valid" I suspect data population on control is from another thread rather than UI. Any reason why? I saw invoke or begin invoke can do some good result, if so how can i utilise it in my code ? or do we have any other alternate mechanism.Following is trace i am getting

                Control control = null;
                object ancestor = Parent;

Where i can use InvokeRequired here, any thoughts?

ponting
  • 105
  • 2
  • 13

2 Answers2

0

Typically you want to use BackgroundWorker for updating the UI in a non-blocking manner.

This allows you to run a repeating or long running background operation that can report back to the UI thread with the details of what needs to update.

BeginInvoke is the .NET 1.1 way of accomplishing the same end, but was largely replaced with background worker.

You also may have the Async options available if you are working with .net 4.5. They may not be a direct enough replacement.

StingyJack
  • 19,041
  • 10
  • 63
  • 122
  • thanks,but do we have any mechanism rather than Background Worker to prevent this issue, i had seen invokerequired will do but i dont know how to use it? – ponting Dec 11 '12 at 19:28
  • 2
    Take a look at some of the other alternatives suggested in the answer on this question. http://stackoverflow.com/q/1132472/16391 – StingyJack Dec 11 '12 at 19:32
-2

Just write this in your form load event .

CheckForIllegalCrossThreadCalls =false;

After this your code properly run ...but keep in mind that cross thread should be safe ..

  • 1
    -1: Please don't do this. Cross-thread UI calls can result in all sorts of weird behavior, so fix it to not do cross-thread calls. – Daniel Rose Dec 13 '12 at 11:08