简体   繁体   中英

Cross-thread operation not valid in C#?

I am creating a background worker thread and Loading data in it and showing in UI. I know the problem is while showing the data in UI(because it is a UI thread) But i get the data from server in form of blocks . Suppose for the first time receive 10 records then i have to update the Ui and then call the next records.

How to resolve this Issue ? Thanks.

If you are using a BackgroundWorker you have to make use of the report progress feature.

What I usually do is to do the work required inside the DoWork method, and when you want to update the GUI, call worker.ReportProgress with the needed data. Then in the report progress method, update the GUI since that method will be run on the GUI thread. Also make sure you set WorkerReportsProgress to true, else you will get an exception when trying to call ReportProgress .

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