How best to run a query in the background so as not to freeze the application (.NET)

My WinForm applications have to execute a complex request with a significant runtime, I do not affect (about 10 minutes) When the request is executed, the user sees "the application is not responding" in the task manager, which really confuses the user, and also not very professional ...

I believe that the request should be executed in different threads or so. We tried some approaches, but you had difficulties with its work (execute the request, force the result of waiting for the main application, return to the main application, the ability to cancel execution, etc.)

I wonder if you have your own / good working solution for this. Code examples will also be very welcome :)

I also believe that there may be some ready-to-use utilities / frameworks that make it easy to accomplish this.

+3
source share
3 answers

If ExecuteQuery, if the method you want to execute, you can do:

void SomeMethod() {
    var thread = new Thread(ExecuteQuery);
    thread.Start();
}

void ExecuteQuery() {
    //Build your query here and execute it.
}

If it ExecuteQueryreceives some parameters, for example:

void ExecuteQuery(string query) {
    //...
}

You can do:

var threadStarter = () => { ExecuteQuery("SELECT * FROM [Table]"); };
var thread = new Thread(ThreadStarter);
thread.Start();

If you want to stop the execution of a background thread, do not call the method thread.Abort(). It will be a killstream, and you do not want this, because some inconsistency may appear in your database.

bool, ExecuteQuery, True, . , , ExecuteQuery, True. , .

, bool volatile

Edit:
, , :

  • (, ..), , (: , , , ).

.

, ? . , , , ...

, , .

+1

- BackgroundWorker. MSDN . , //etc. , .

API- Task 4.0; , () . BackgroundWorker ( ).

+1

If you are new to threads and the task is simple (as it seems), you should try using the standard background worker component .

0
source

All Articles