How to make form events to use benefits from async?









up vote
0
down vote

favorite












I have a simple Form with buton and one event button1_Click:



public Form1 : Form

public Form1()

...
this.button1.Click += new System.EventHandler(this.button1_Click);


private async Task button1_Click(object sender, EventArgs e) // line A





In line A I would like to try use async Task return type rather than void and I encounter error message from compiler. How I can make this code run?










share|improve this question

















  • 3




    You can use asyns void in an event handler. Async/Await - Best Practices in Asynchronous Programming.
    – Jimi
    yesterday















up vote
0
down vote

favorite












I have a simple Form with buton and one event button1_Click:



public Form1 : Form

public Form1()

...
this.button1.Click += new System.EventHandler(this.button1_Click);


private async Task button1_Click(object sender, EventArgs e) // line A





In line A I would like to try use async Task return type rather than void and I encounter error message from compiler. How I can make this code run?










share|improve this question

















  • 3




    You can use asyns void in an event handler. Async/Await - Best Practices in Asynchronous Programming.
    – Jimi
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a simple Form with buton and one event button1_Click:



public Form1 : Form

public Form1()

...
this.button1.Click += new System.EventHandler(this.button1_Click);


private async Task button1_Click(object sender, EventArgs e) // line A





In line A I would like to try use async Task return type rather than void and I encounter error message from compiler. How I can make this code run?










share|improve this question













I have a simple Form with buton and one event button1_Click:



public Form1 : Form

public Form1()

...
this.button1.Click += new System.EventHandler(this.button1_Click);


private async Task button1_Click(object sender, EventArgs e) // line A





In line A I would like to try use async Task return type rather than void and I encounter error message from compiler. How I can make this code run?







c# winforms






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









Piotr Wojcik

1810




1810







  • 3




    You can use asyns void in an event handler. Async/Await - Best Practices in Asynchronous Programming.
    – Jimi
    yesterday













  • 3




    You can use asyns void in an event handler. Async/Await - Best Practices in Asynchronous Programming.
    – Jimi
    yesterday








3




3




You can use asyns void in an event handler. Async/Await - Best Practices in Asynchronous Programming.
– Jimi
yesterday





You can use asyns void in an event handler. Async/Await - Best Practices in Asynchronous Programming.
– Jimi
yesterday













3 Answers
3






active

oldest

votes

















up vote
2
down vote













That event handler is a delegate with void return type, which means you can't add methods with Task result to it.



However you can also mark void methods with async so you should just replace the Task with void.






share|improve this answer




















  • async void is a bad practice. In multi-thread programming it has a flow. Please read the circumstances of async with void
    – Hassaan
    yesterday






  • 1




    You don't have a chance to use anything else with void event handlers and in wpf and winforms you only have those. It can be dangerous if you don't know what are you doing and how it should be used, but it's true for anything else too.
    – Péter Csajtai
    yesterday


















up vote
0
down vote













We can check this code:



 public partial class Form1 : Form

public Form1()

InitializeComponent();
this.button1.Click += (s, e) => Button1_Click(s, e);
//this.button1.Click += Button1_Click1;
MessageBox.Show("The End!");

private async Task Button1_Click(object sender, EventArgs e)

await Task.Delay(5000);
MessageBox.Show("With Sync");


private void Button1_Click1(object sender, EventArgs e)

MessageBox.Show("Without async");







share|improve this answer




















  • I think you should swap MessageBoxes between Button1_Click and Button1_Click1.
    – Piotr Wojcik
    yesterday










  • This is just obscuring the fact that the event handler is void returning by making it an anonymous method. You still have the void event handier. This is in fact far worse, as it behaves the same but could very easily mislead the reader into thinking this isn't a fire and forget operation when it in fact is.
    – Servy
    yesterday

















up vote
0
down vote














How I can make this code run?




It cannot be done because the async Task signature is not implemented for UI event handlers in .net. (make sure to read this post: Why do event handlers always have a return type of void?)



The general consensus is that, in general, a return value of an event handler has not a well defined purpose. Although IMO it would make sense to be able to fire off all the handlers async, but then again, that would be exactly the case if you wouldn't await the logic inside it.



Of course you could overcome the problem by putting the event logic in another class. Most of the time this is actual a good idea anyhow.



Note: do not use it like this, it's just to explain it



public Form1 : Form

public Form1()

...
this.button1.Click += new System.EventHandler(this.button1_Click);


private async void button1_Click(object sender, EventArgs e) // line A

await YourLogic(e.foo); //optional configure await.


private async Task YourLogic(your parameters)

// do stuff




See also:



Why do event handlers always have a return type of void?



async/await - when to return a Task vs void?



https://msdn.microsoft.com/en-us/magazine/jj991977.aspx



How do I await events in C#?





How to make form events to use benefits from async?




So, basically it already does, but check out this post:
How to 'await' raising an EventHandler event






share|improve this answer






















  • Does await make this asonchrynous call synchronous? Async make promise that YourLogic would be finished but soon as you call that method you simply wait until YourLogic finish.
    – Piotr Wojcik
    yesterday






  • 1




    That's a bit of a trick question. In short: what await does, is that it executes the code async (so your ui remains responsive) but resumes from that point afterward. It is like having your function split up into 2 parts.
    – Stefan
    yesterday










  • @PéterCsajtai thanks for the typo correction ;-)
    – Stefan
    yesterday










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224271%2fhow-to-make-form-events-to-use-benefits-from-async%23new-answer', 'question_page');

);

Post as a guest






























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













That event handler is a delegate with void return type, which means you can't add methods with Task result to it.



However you can also mark void methods with async so you should just replace the Task with void.






share|improve this answer




















  • async void is a bad practice. In multi-thread programming it has a flow. Please read the circumstances of async with void
    – Hassaan
    yesterday






  • 1




    You don't have a chance to use anything else with void event handlers and in wpf and winforms you only have those. It can be dangerous if you don't know what are you doing and how it should be used, but it's true for anything else too.
    – Péter Csajtai
    yesterday















up vote
2
down vote













That event handler is a delegate with void return type, which means you can't add methods with Task result to it.



However you can also mark void methods with async so you should just replace the Task with void.






share|improve this answer




















  • async void is a bad practice. In multi-thread programming it has a flow. Please read the circumstances of async with void
    – Hassaan
    yesterday






  • 1




    You don't have a chance to use anything else with void event handlers and in wpf and winforms you only have those. It can be dangerous if you don't know what are you doing and how it should be used, but it's true for anything else too.
    – Péter Csajtai
    yesterday













up vote
2
down vote










up vote
2
down vote









That event handler is a delegate with void return type, which means you can't add methods with Task result to it.



However you can also mark void methods with async so you should just replace the Task with void.






share|improve this answer












That event handler is a delegate with void return type, which means you can't add methods with Task result to it.



However you can also mark void methods with async so you should just replace the Task with void.







share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









Péter Csajtai

49616




49616











  • async void is a bad practice. In multi-thread programming it has a flow. Please read the circumstances of async with void
    – Hassaan
    yesterday






  • 1




    You don't have a chance to use anything else with void event handlers and in wpf and winforms you only have those. It can be dangerous if you don't know what are you doing and how it should be used, but it's true for anything else too.
    – Péter Csajtai
    yesterday

















  • async void is a bad practice. In multi-thread programming it has a flow. Please read the circumstances of async with void
    – Hassaan
    yesterday






  • 1




    You don't have a chance to use anything else with void event handlers and in wpf and winforms you only have those. It can be dangerous if you don't know what are you doing and how it should be used, but it's true for anything else too.
    – Péter Csajtai
    yesterday
















async void is a bad practice. In multi-thread programming it has a flow. Please read the circumstances of async with void
– Hassaan
yesterday




async void is a bad practice. In multi-thread programming it has a flow. Please read the circumstances of async with void
– Hassaan
yesterday




1




1




You don't have a chance to use anything else with void event handlers and in wpf and winforms you only have those. It can be dangerous if you don't know what are you doing and how it should be used, but it's true for anything else too.
– Péter Csajtai
yesterday





You don't have a chance to use anything else with void event handlers and in wpf and winforms you only have those. It can be dangerous if you don't know what are you doing and how it should be used, but it's true for anything else too.
– Péter Csajtai
yesterday













up vote
0
down vote













We can check this code:



 public partial class Form1 : Form

public Form1()

InitializeComponent();
this.button1.Click += (s, e) => Button1_Click(s, e);
//this.button1.Click += Button1_Click1;
MessageBox.Show("The End!");

private async Task Button1_Click(object sender, EventArgs e)

await Task.Delay(5000);
MessageBox.Show("With Sync");


private void Button1_Click1(object sender, EventArgs e)

MessageBox.Show("Without async");







share|improve this answer




















  • I think you should swap MessageBoxes between Button1_Click and Button1_Click1.
    – Piotr Wojcik
    yesterday










  • This is just obscuring the fact that the event handler is void returning by making it an anonymous method. You still have the void event handier. This is in fact far worse, as it behaves the same but could very easily mislead the reader into thinking this isn't a fire and forget operation when it in fact is.
    – Servy
    yesterday














up vote
0
down vote













We can check this code:



 public partial class Form1 : Form

public Form1()

InitializeComponent();
this.button1.Click += (s, e) => Button1_Click(s, e);
//this.button1.Click += Button1_Click1;
MessageBox.Show("The End!");

private async Task Button1_Click(object sender, EventArgs e)

await Task.Delay(5000);
MessageBox.Show("With Sync");


private void Button1_Click1(object sender, EventArgs e)

MessageBox.Show("Without async");







share|improve this answer




















  • I think you should swap MessageBoxes between Button1_Click and Button1_Click1.
    – Piotr Wojcik
    yesterday










  • This is just obscuring the fact that the event handler is void returning by making it an anonymous method. You still have the void event handier. This is in fact far worse, as it behaves the same but could very easily mislead the reader into thinking this isn't a fire and forget operation when it in fact is.
    – Servy
    yesterday












up vote
0
down vote










up vote
0
down vote









We can check this code:



 public partial class Form1 : Form

public Form1()

InitializeComponent();
this.button1.Click += (s, e) => Button1_Click(s, e);
//this.button1.Click += Button1_Click1;
MessageBox.Show("The End!");

private async Task Button1_Click(object sender, EventArgs e)

await Task.Delay(5000);
MessageBox.Show("With Sync");


private void Button1_Click1(object sender, EventArgs e)

MessageBox.Show("Without async");







share|improve this answer












We can check this code:



 public partial class Form1 : Form

public Form1()

InitializeComponent();
this.button1.Click += (s, e) => Button1_Click(s, e);
//this.button1.Click += Button1_Click1;
MessageBox.Show("The End!");

private async Task Button1_Click(object sender, EventArgs e)

await Task.Delay(5000);
MessageBox.Show("With Sync");


private void Button1_Click1(object sender, EventArgs e)

MessageBox.Show("Without async");








share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









ideepak04

362




362











  • I think you should swap MessageBoxes between Button1_Click and Button1_Click1.
    – Piotr Wojcik
    yesterday










  • This is just obscuring the fact that the event handler is void returning by making it an anonymous method. You still have the void event handier. This is in fact far worse, as it behaves the same but could very easily mislead the reader into thinking this isn't a fire and forget operation when it in fact is.
    – Servy
    yesterday
















  • I think you should swap MessageBoxes between Button1_Click and Button1_Click1.
    – Piotr Wojcik
    yesterday










  • This is just obscuring the fact that the event handler is void returning by making it an anonymous method. You still have the void event handier. This is in fact far worse, as it behaves the same but could very easily mislead the reader into thinking this isn't a fire and forget operation when it in fact is.
    – Servy
    yesterday















I think you should swap MessageBoxes between Button1_Click and Button1_Click1.
– Piotr Wojcik
yesterday




I think you should swap MessageBoxes between Button1_Click and Button1_Click1.
– Piotr Wojcik
yesterday












This is just obscuring the fact that the event handler is void returning by making it an anonymous method. You still have the void event handier. This is in fact far worse, as it behaves the same but could very easily mislead the reader into thinking this isn't a fire and forget operation when it in fact is.
– Servy
yesterday




This is just obscuring the fact that the event handler is void returning by making it an anonymous method. You still have the void event handier. This is in fact far worse, as it behaves the same but could very easily mislead the reader into thinking this isn't a fire and forget operation when it in fact is.
– Servy
yesterday










up vote
0
down vote














How I can make this code run?




It cannot be done because the async Task signature is not implemented for UI event handlers in .net. (make sure to read this post: Why do event handlers always have a return type of void?)



The general consensus is that, in general, a return value of an event handler has not a well defined purpose. Although IMO it would make sense to be able to fire off all the handlers async, but then again, that would be exactly the case if you wouldn't await the logic inside it.



Of course you could overcome the problem by putting the event logic in another class. Most of the time this is actual a good idea anyhow.



Note: do not use it like this, it's just to explain it



public Form1 : Form

public Form1()

...
this.button1.Click += new System.EventHandler(this.button1_Click);


private async void button1_Click(object sender, EventArgs e) // line A

await YourLogic(e.foo); //optional configure await.


private async Task YourLogic(your parameters)

// do stuff




See also:



Why do event handlers always have a return type of void?



async/await - when to return a Task vs void?



https://msdn.microsoft.com/en-us/magazine/jj991977.aspx



How do I await events in C#?





How to make form events to use benefits from async?




So, basically it already does, but check out this post:
How to 'await' raising an EventHandler event






share|improve this answer






















  • Does await make this asonchrynous call synchronous? Async make promise that YourLogic would be finished but soon as you call that method you simply wait until YourLogic finish.
    – Piotr Wojcik
    yesterday






  • 1




    That's a bit of a trick question. In short: what await does, is that it executes the code async (so your ui remains responsive) but resumes from that point afterward. It is like having your function split up into 2 parts.
    – Stefan
    yesterday










  • @PéterCsajtai thanks for the typo correction ;-)
    – Stefan
    yesterday














up vote
0
down vote














How I can make this code run?




It cannot be done because the async Task signature is not implemented for UI event handlers in .net. (make sure to read this post: Why do event handlers always have a return type of void?)



The general consensus is that, in general, a return value of an event handler has not a well defined purpose. Although IMO it would make sense to be able to fire off all the handlers async, but then again, that would be exactly the case if you wouldn't await the logic inside it.



Of course you could overcome the problem by putting the event logic in another class. Most of the time this is actual a good idea anyhow.



Note: do not use it like this, it's just to explain it



public Form1 : Form

public Form1()

...
this.button1.Click += new System.EventHandler(this.button1_Click);


private async void button1_Click(object sender, EventArgs e) // line A

await YourLogic(e.foo); //optional configure await.


private async Task YourLogic(your parameters)

// do stuff




See also:



Why do event handlers always have a return type of void?



async/await - when to return a Task vs void?



https://msdn.microsoft.com/en-us/magazine/jj991977.aspx



How do I await events in C#?





How to make form events to use benefits from async?




So, basically it already does, but check out this post:
How to 'await' raising an EventHandler event






share|improve this answer






















  • Does await make this asonchrynous call synchronous? Async make promise that YourLogic would be finished but soon as you call that method you simply wait until YourLogic finish.
    – Piotr Wojcik
    yesterday






  • 1




    That's a bit of a trick question. In short: what await does, is that it executes the code async (so your ui remains responsive) but resumes from that point afterward. It is like having your function split up into 2 parts.
    – Stefan
    yesterday










  • @PéterCsajtai thanks for the typo correction ;-)
    – Stefan
    yesterday












up vote
0
down vote










up vote
0
down vote










How I can make this code run?




It cannot be done because the async Task signature is not implemented for UI event handlers in .net. (make sure to read this post: Why do event handlers always have a return type of void?)



The general consensus is that, in general, a return value of an event handler has not a well defined purpose. Although IMO it would make sense to be able to fire off all the handlers async, but then again, that would be exactly the case if you wouldn't await the logic inside it.



Of course you could overcome the problem by putting the event logic in another class. Most of the time this is actual a good idea anyhow.



Note: do not use it like this, it's just to explain it



public Form1 : Form

public Form1()

...
this.button1.Click += new System.EventHandler(this.button1_Click);


private async void button1_Click(object sender, EventArgs e) // line A

await YourLogic(e.foo); //optional configure await.


private async Task YourLogic(your parameters)

// do stuff




See also:



Why do event handlers always have a return type of void?



async/await - when to return a Task vs void?



https://msdn.microsoft.com/en-us/magazine/jj991977.aspx



How do I await events in C#?





How to make form events to use benefits from async?




So, basically it already does, but check out this post:
How to 'await' raising an EventHandler event






share|improve this answer















How I can make this code run?




It cannot be done because the async Task signature is not implemented for UI event handlers in .net. (make sure to read this post: Why do event handlers always have a return type of void?)



The general consensus is that, in general, a return value of an event handler has not a well defined purpose. Although IMO it would make sense to be able to fire off all the handlers async, but then again, that would be exactly the case if you wouldn't await the logic inside it.



Of course you could overcome the problem by putting the event logic in another class. Most of the time this is actual a good idea anyhow.



Note: do not use it like this, it's just to explain it



public Form1 : Form

public Form1()

...
this.button1.Click += new System.EventHandler(this.button1_Click);


private async void button1_Click(object sender, EventArgs e) // line A

await YourLogic(e.foo); //optional configure await.


private async Task YourLogic(your parameters)

// do stuff




See also:



Why do event handlers always have a return type of void?



async/await - when to return a Task vs void?



https://msdn.microsoft.com/en-us/magazine/jj991977.aspx



How do I await events in C#?





How to make form events to use benefits from async?




So, basically it already does, but check out this post:
How to 'await' raising an EventHandler event







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









Stefan

7,73873559




7,73873559











  • Does await make this asonchrynous call synchronous? Async make promise that YourLogic would be finished but soon as you call that method you simply wait until YourLogic finish.
    – Piotr Wojcik
    yesterday






  • 1




    That's a bit of a trick question. In short: what await does, is that it executes the code async (so your ui remains responsive) but resumes from that point afterward. It is like having your function split up into 2 parts.
    – Stefan
    yesterday










  • @PéterCsajtai thanks for the typo correction ;-)
    – Stefan
    yesterday
















  • Does await make this asonchrynous call synchronous? Async make promise that YourLogic would be finished but soon as you call that method you simply wait until YourLogic finish.
    – Piotr Wojcik
    yesterday






  • 1




    That's a bit of a trick question. In short: what await does, is that it executes the code async (so your ui remains responsive) but resumes from that point afterward. It is like having your function split up into 2 parts.
    – Stefan
    yesterday










  • @PéterCsajtai thanks for the typo correction ;-)
    – Stefan
    yesterday















Does await make this asonchrynous call synchronous? Async make promise that YourLogic would be finished but soon as you call that method you simply wait until YourLogic finish.
– Piotr Wojcik
yesterday




Does await make this asonchrynous call synchronous? Async make promise that YourLogic would be finished but soon as you call that method you simply wait until YourLogic finish.
– Piotr Wojcik
yesterday




1




1




That's a bit of a trick question. In short: what await does, is that it executes the code async (so your ui remains responsive) but resumes from that point afterward. It is like having your function split up into 2 parts.
– Stefan
yesterday




That's a bit of a trick question. In short: what await does, is that it executes the code async (so your ui remains responsive) but resumes from that point afterward. It is like having your function split up into 2 parts.
– Stefan
yesterday












@PéterCsajtai thanks for the typo correction ;-)
– Stefan
yesterday




@PéterCsajtai thanks for the typo correction ;-)
– Stefan
yesterday

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224271%2fhow-to-make-form-events-to-use-benefits-from-async%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20