Reputation: 133
I am working on a Winforms C#.net application.
public partial class FormMain : Form
{
private bool cancelQueryWaitRequested = false;
public FormMain()
{
InitializeComponent();
}
private async void btnQueueStart_Click(object sender, EventArgs e)
{
QueryFinishedWait();
}
private int QueryFinishedWait()
{
int i = 1;
reQueueUIStart();
groupWaitQueue.Visible = true;
for (i = 0; i < 20; i++)
{
lblQueueWaitSeconds.Text = (20 - i).ToString();
Thread.Sleep(1000);
if (cancelQueryWaitRequested)
break;
}
groupWaitQueue.Visible = false;
reQueueUIStop();
return i;
}
}
When I execute this, the application goes "Not responding" and I can see the Processor usage goes very high.
But when I run the same loop within btnQueueStart_Click
it works properly.
I know I am doing something wrong, still can't figure what.
Thanks in advance.
Upvotes: 0
Views: 558
Reputation: 273721
But when I run the same loop within btnQueueStart_Click it works properly.
That's because the event handler is async, but you destroy that when calling your method.
private async void btnQueueStart_Click(object sender, EventArgs e)
{
//QueryFinishedWait();
await QueryFinishedWait();
}
async private Task<int> QueryFinishedWait()
{
... // await something
}
But it seems you're missng another piece of the async puzzle. And please note that waiting with Sleep()
is not recommended.
Upvotes: 1