How best execute query in background to not freeze application (.NET)

前端 未结 3 1376
刺人心
刺人心 2020-12-11 23:26

My WinForm apps needs to execute complex query with significant execution time, I have no influence (about 10mins) When query is executing user sees \'application not respon

3条回答
  •  执笔经年
    2020-12-12 00:10

    The simplest approach here would be to do that work from a BackgroundWorker. MSDN has examples for this. This then executes on a worker thread, with events for completion/error/etc. It can also support cancel, but your operation needs to be coded to be interruptable.

    Another approach is the Task API in 4.0; but if you use this you'll need to get back to the UI thread (afterwards) yourself. With BackgroundWorker this is automatic (the events are raised on the UI thread).

提交回复
热议问题