Is it possible to hang up the creation of a task on a button, so that it does not end when the button is exited, or do you have to store the tasks in some list?

Scenario:

I click on the button, a background long task is created that does not interrupt the UI.

If you do this:

private void btnToExcel_Click(object sender, EventArgs e) { btnToExcel.Enabled = false; var excl=new ExcelCreator(tbConvDbPath.Text, tbPathToExcel.Text, AddText); taskList.Add(Task.Run(()=> excl.Create())); btnToExcel.Enabled = true;//После завершения задачи } 

Then only 1 instruction is executed in the method ...

  • "only 1 instruction is executed" Combine the instructions you need into a block using {} - 4per
  • @ 3per, only one instruction is executed in the excl.Create () method ... - iluxa1810
  • replace excl.Create () with {excl.Create (); next_instrution;} - 4per

3 answers 3

Attention! The answer is not entirely successful, I did not fully understand the author, but the answer is accepted. Look better answer from @andreycha

 private void btnToExcel_Click(object sender, EventArgs e) { btnToExcel.Enabled = false; var excl=new ExcelCreator(tbConvDbPath.Text, tbPathToExcel.Text, AddText); taskList.Add(Task.Run(()=> { excl.Create(); btnToExcel.Invoke(new Action (() => { btnToExcel.Enabled = true;//После завершения задачи } })); } 
  • The question is, how to make the exception see the main stream in the stream? - iluxa1810
  • for example, terminate the main thread. - iluxa1810
  • 3per, strange, but I do not. I tried explicitly at the beginning inside excl.Create () to call throw new Exception and the main thread doesn’t care, although I did not set any try / cath. - iluxa1810
  • perhaps Task intercepts an exception and does not forward to the main thread. - iluxa1810
  • one
    The moderator can not take and uncheck, and delete in this case is not quite correct. But a good option may be some message at the beginning of the question: "Attention, the answer does not quite correspond to the question, therefore, and therefore ...". Typically, such messages are allocated block block to see better. - Nick Volynkin

The most natural option is to use async/await :

 private async void btnToExcel_Click(object sender, EventArgs e) { btnToExcel.Enabled = false; var excl = new ExcelCreator(tbConvDbPath.Text, tbPathToExcel.Text, AddText); var task = Task.Run(() => excl.Create()); taskList.Add(task); try { await task; } catch (Exception e) { // ваша логика обработки исключения } finally { btnToExcel.Enabled = true; } } 

PS By the way, why do you need taskList ? What are you doing with him then?

    Use the thread pool:

     ThreadPool.QueueUserWorkItem(obj => "Ваш код", 5); 

    just add your task and it will be executed in the background thread

    • And the list of tasks is better to leave early? - iluxa1810
    • If you need the result of their execution or control of performance, then it is better to use the Task mechanism, and if not, then the list is not needed. Those. if you have a “started-forgot” task, then the list is not needed - Alexsandr Ter
    • Is it possible to set an example within the framework of the script that I described above? Or even fix the code that I gave in the example? - iluxa1810
    • And better still like this: When you click on the button, the button is transferred to Enabled = false, and when the task is completed in Enabled = true - iluxa1810
    • you just have to take into account that the button may belong to another thread, so use this. Invoke - Alexsandr Ter