how to run process in background in .net using c#
I have to run process in background using thread and background worker. this process do the task of retrieve data from database,it retrieve successfully by I can not display that data into 开发者_运维技巧datagridview, there is give some data error event.
BackgroundWorker has a built in callback method of RunWorkerCompleted
that you wire into and it handles the cross thread marshaling to the UI thread automatically. Before you call RunWorkerAsync
you can wire into the the RunWorkerCompleted
event.
backgroundWorker1.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(
backgroundWorker1_RunWorkerCompleted);
within the backgroundWorker1_RunWorkerCompleted
you can bind your grid or set the DataSource property to the DataTable and it will be on the UI thread.
If you have already done this and are getting errors it might just be an unhandled exception within the DoWork/threaded code itself. If that is the case wrap the RunWorkerAsync
call in a try catch and see what you get.
Hope that help...
May be you are unable to modify the DataGridView Control. Set CheckForCrossThreadcalls = false ,it may works
精彩评论