You have to use backgroundworker , this is exactly what you want.
Drag it from the toolbar or create in code. It supports cancellation, reports on progress made, notifies completion and knows whether it works or not.
Here is an example.
void method(){ BackgroundWorker worker = new BackgroundWorker(); worker.RunWorkerCompleted += worker_RunWorkerCompleted; worker.ProgressChanged += worker_ProgressChanged; worker.DoWork += worker_DoWork; worker.WorkerSupportsCancellation = true; if(!worker.IsBusy) { worker.RunWorkerAsync(); } } void worker_DoWork(object sender, DoWorkEventArgs e) {
Important things to note: never use resources in the DoWork method that are not created inside it. So pass what you need in the background worker as โArgumentsโ. And the things created by the creator of backgroundwork should not be set to the global ether variable, pass the result.
When canceled, RunWorkCompleted will also be launched. Now the database query is already running, so it still works, even when your application has lost all the resources for it.
To undo this, we will need to find out how you are executing the request, for example, @ S. Akbari is one way. Entity Framework 6 also supports cancellation.
To do this: check this when using Queryable
here is another example
Or this solution without Entity Framework.
source share