简体   繁体   中英

Error :System.InvalidOperationException: Cross-thread operation not valid

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?

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.

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 ..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM