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 responding’ in task manager, which is really confusing to user, also not very professional…
I believe that query shall be executed in different thread or so. Have tried some approaches but have difficulties to make it really working (execute query, force main application wait result, return back to main app, possibility to cancel execution etc)
I wonder if you have own / good working solution for that. Code samples would be also very welcome 🙂
Also I believe there might exist some ready to use utilities / frameworks allowing simple execution of that.
If
ExecuteQueryif the method you want to execute, you can do:If
ExecuteQueryreceives some parameters, like:You can do:
If you want to stop the execution of the background thread, avoid calling
thread.Abort()method. That willkillthe thread, and you do not want this, because some incosistency could appear in your database.Instead, you can have a
boolvariable visible fromExecuteQueryand from outside you can set it toTruewhen you want to stop it. Then all you have to do is check in some parts of the code insideExecuteQueryif that variable is stillTrue. Otherwise, do some rollback to maintain the database stable.Be sure you set that
boolvariablevolatileEdit:
If you want the UI to wait from the background thread, I usually do:
After finished the thread, you can stop progress bar and enable controls again.
How to know when the thread finished? You can use Events for that. Just create an event and fire it when it finishes, and do whatever you want inside the event handler…
Be sure you’re accessing correctly to UI controls from the background thread, otherwise it will give you an error.