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?
c# winforms
add a comment |
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?
c# winforms
3
You can useasyns void
in an event handler. Async/Await - Best Practices in Asynchronous Programming.
– Jimi
yesterday
add a comment |
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?
c# winforms
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
c# winforms
asked yesterday
Piotr Wojcik
1810
1810
3
You can useasyns void
in an event handler. Async/Await - Best Practices in Asynchronous Programming.
– Jimi
yesterday
add a comment |
3
You can useasyns 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
add a comment |
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
.
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
add a comment |
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");
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
add a comment |
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
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: whatawait
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
add a comment |
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
.
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
add a comment |
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
.
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
add a comment |
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
.
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
.
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
add a comment |
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
add a comment |
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");
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
add a comment |
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");
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
add a comment |
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");
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");
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
add a comment |
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
add a comment |
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
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: whatawait
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
add a comment |
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
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: whatawait
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
add a comment |
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
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
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: whatawait
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
add a comment |
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: whatawait
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
You can use
asyns void
in an event handler. Async/Await - Best Practices in Asynchronous Programming.– Jimi
yesterday