Difference between using Task.Yield() in a User Interface and Console App - c#

I'm trying to asynchronously show a progress form that says the application is running while the actual application is running.
As following this question, I have the following:
Main Form:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
async Task<int> LoadDataAsync()
{
await Task.Delay(2000);
return 42;
}
private async void Run_Click(object sender, EventArgs e)
{
var runningForm = new RunningForm();
runningForm.ShowRunning();
var progressFormTask = runningForm.ShowDialogAsync();
var data = await LoadDataAsync();
runningForm.Close();
await progressFormTask;
MessageBox.Show(data.ToString());
}
}
Progress Form
public partial class RunningForm : Form
{
private readonly SynchronizationContext synchronizationContext;
public RunningForm()
{
InitializeComponent();
synchronizationContext = SynchronizationContext.Current;
}
public async void ShowRunning()
{
this.RunningLabel.Text = "Running";
int dots = 0;
await Task.Run(() =>
{
while (true)
{
UpadateUi($"Running{new string('.', dots)}");
Thread.Sleep(300);
dots = (dots == 3) ? 0 : dots + 1;
}
});
}
public void UpadateUi(string text)
{
synchronizationContext.Post(
new SendOrPostCallback(o =>
{
this.RunningLabel.Text = text;
}),
text);
}
public void CloseThread()
{
synchronizationContext.Post(
new SendOrPostCallback(o =>
{
this.Close();
}),
null);
}
}
internal static class DialogExt
{
public static async Task<DialogResult> ShowDialogAsync(this Form form)
{
await Task.Yield();
if (form.IsDisposed)
{
return DialogResult.OK;
}
return form.ShowDialog();
}
}
The above works fine, but it doesn't work when I'm calling from outside of another from. This is my console app:
class Program
{
static void Main(string[] args)
{
new Test().Run();
Console.ReadLine();
}
}
class Test
{
private RunningForm runningForm;
public async void Run()
{
var runningForm = new RunningForm();
runningForm.ShowRunning();
var progressFormTask = runningForm.ShowDialogAsync();
var data = await LoadDataAsync();
runningForm.CloseThread();
await progressFormTask;
MessageBox.Show(data.ToString());
}
async Task<int> LoadDataAsync()
{
await Task.Delay(2000);
return 42;
}
}
Watching what happens with the debugger, the process gets to await Task.Yield() and never progresses to return form.ShowDialog() and thus you never see the RunningForm. The process then goes to LoadDataAsync() and hangs forever on await Task.Delay(2000).
Why is this happening? Does it have something to do with how Tasks are prioritized (ie: Task.Yield())?

Watching what happens with the debugger, the process gets to await
Task.Yield() and never progresses to return form.ShowDialog() and thus
you never see the RunningForm. The process then goes to
LoadDataAsync() and hangs forever on await Task.Delay(2000).
Why is this happening?
What happens here is that when you do var runningForm = new RunningForm() on a console thread without any synchronization context (System.Threading.SynchronizationContext.Current is null), it implicitly creates an instance of WindowsFormsSynchronizationContext and installs it on the current thread, more on this here.
Then, when you hit await Task.Yield(), the ShowDialogAsync method returns to the caller and the await continuation is posted to that new synchronization context. However, the continuation never gets a chance to be invoked, because the current thread doesn't run a message loop and the posted messages don't get pumped. There isn't a deadlock, but the code after await Task.Yield() is never executed, so the dialog doesn't even get shown. The same is true about await Task.Delay(2000).
I'm more interested in learning why it works for WinForms and not for
Console Applications.
You need a UI thread with a message loop in your console app. Try refactoring your console app like this:
public void Run()
{
var runningForm = new RunningForm();
runningForm.Loaded += async delegate
{
runningForm.ShowRunning();
var progressFormTask = runningForm.ShowDialogAsync();
var data = await LoadDataAsync();
runningForm.Close();
await progressFormTask;
MessageBox.Show(data.ToString());
};
System.Windows.Forms.Application.Run(runningForm);
}
Here, the job of Application.Run is to start a modal message loop (and install WindowsFormsSynchronizationContext on the current thread) then show the form. The runningForm.Loaded async event handler is invoked on that synchronization context, so the logic inside it should work just as expected.
That however makes Test.Run a synchronous method, i. e., it only returns when the form is closed and the message loop has ended. If this is not what you want, you'd have to create a separate thread to run your message loop, something like I do with MessageLoopApartment here.
That said, in a typical WinForms or WPF application you should almost never need a secondary UI thread.

Related

Wait for a specific result from Task then run another method in C#

I have a WPF app running on .net 6 and an external device connected to it.
Initializing the device sometimes fails and I don't want to hold the UI thread trying to initialize it.
I want to run the following method (_device.Init()) in an async fashion and when it returns true, run Start() method.
edit: run it until it returns true from the _device.Init() method, not true for finishing the task
Is there a built-in functionality to do it with tasks? or any other "best practice" way?
Thank you :)
SomeDevice _device = new();
public async void Init()
{
// some other code
while (Task.Run(() => _device.Init()).Result == false)
{
}
Start();
}
public void Start()
{
// some other code
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
_device.Start();
}));
}
Instead of getting the Result of the Task (which may block the UI thread) you should await the Task:
public async void Init()
{
// some other code
while (!await Task.Run(() => _device.Init()))
{
}
Start();
}
The method should also be awaitable and be awaited when called, e.g. in an async Loaded event handler:
public async Task Init()
{
// some other code
while (!await Task.Run(() => _device.Init()))
{
}
Start();
}
...
await Init();
public async void Init()
{
var task = _device.Init();
//do work here
await task;
Start();
}
Should do the trick, it'll do the work and then wait for the task to complete before going to Start();
If you want to simply wait for init to finish and then run start it's even simpler with
await _device.Init().ContinueWith((x) => { Start();})

Why does creating a winform object in a threadpool thread break async methods? [duplicate]

I'm trying to asynchronously show a progress form that says the application is running while the actual application is running.
As following this question, I have the following:
Main Form:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
async Task<int> LoadDataAsync()
{
await Task.Delay(2000);
return 42;
}
private async void Run_Click(object sender, EventArgs e)
{
var runningForm = new RunningForm();
runningForm.ShowRunning();
var progressFormTask = runningForm.ShowDialogAsync();
var data = await LoadDataAsync();
runningForm.Close();
await progressFormTask;
MessageBox.Show(data.ToString());
}
}
Progress Form
public partial class RunningForm : Form
{
private readonly SynchronizationContext synchronizationContext;
public RunningForm()
{
InitializeComponent();
synchronizationContext = SynchronizationContext.Current;
}
public async void ShowRunning()
{
this.RunningLabel.Text = "Running";
int dots = 0;
await Task.Run(() =>
{
while (true)
{
UpadateUi($"Running{new string('.', dots)}");
Thread.Sleep(300);
dots = (dots == 3) ? 0 : dots + 1;
}
});
}
public void UpadateUi(string text)
{
synchronizationContext.Post(
new SendOrPostCallback(o =>
{
this.RunningLabel.Text = text;
}),
text);
}
public void CloseThread()
{
synchronizationContext.Post(
new SendOrPostCallback(o =>
{
this.Close();
}),
null);
}
}
internal static class DialogExt
{
public static async Task<DialogResult> ShowDialogAsync(this Form form)
{
await Task.Yield();
if (form.IsDisposed)
{
return DialogResult.OK;
}
return form.ShowDialog();
}
}
The above works fine, but it doesn't work when I'm calling from outside of another from. This is my console app:
class Program
{
static void Main(string[] args)
{
new Test().Run();
Console.ReadLine();
}
}
class Test
{
private RunningForm runningForm;
public async void Run()
{
var runningForm = new RunningForm();
runningForm.ShowRunning();
var progressFormTask = runningForm.ShowDialogAsync();
var data = await LoadDataAsync();
runningForm.CloseThread();
await progressFormTask;
MessageBox.Show(data.ToString());
}
async Task<int> LoadDataAsync()
{
await Task.Delay(2000);
return 42;
}
}
Watching what happens with the debugger, the process gets to await Task.Yield() and never progresses to return form.ShowDialog() and thus you never see the RunningForm. The process then goes to LoadDataAsync() and hangs forever on await Task.Delay(2000).
Why is this happening? Does it have something to do with how Tasks are prioritized (ie: Task.Yield())?
Watching what happens with the debugger, the process gets to await
Task.Yield() and never progresses to return form.ShowDialog() and thus
you never see the RunningForm. The process then goes to
LoadDataAsync() and hangs forever on await Task.Delay(2000).
Why is this happening?
What happens here is that when you do var runningForm = new RunningForm() on a console thread without any synchronization context (System.Threading.SynchronizationContext.Current is null), it implicitly creates an instance of WindowsFormsSynchronizationContext and installs it on the current thread, more on this here.
Then, when you hit await Task.Yield(), the ShowDialogAsync method returns to the caller and the await continuation is posted to that new synchronization context. However, the continuation never gets a chance to be invoked, because the current thread doesn't run a message loop and the posted messages don't get pumped. There isn't a deadlock, but the code after await Task.Yield() is never executed, so the dialog doesn't even get shown. The same is true about await Task.Delay(2000).
I'm more interested in learning why it works for WinForms and not for
Console Applications.
You need a UI thread with a message loop in your console app. Try refactoring your console app like this:
public void Run()
{
var runningForm = new RunningForm();
runningForm.Loaded += async delegate
{
runningForm.ShowRunning();
var progressFormTask = runningForm.ShowDialogAsync();
var data = await LoadDataAsync();
runningForm.Close();
await progressFormTask;
MessageBox.Show(data.ToString());
};
System.Windows.Forms.Application.Run(runningForm);
}
Here, the job of Application.Run is to start a modal message loop (and install WindowsFormsSynchronizationContext on the current thread) then show the form. The runningForm.Loaded async event handler is invoked on that synchronization context, so the logic inside it should work just as expected.
That however makes Test.Run a synchronous method, i. e., it only returns when the form is closed and the message loop has ended. If this is not what you want, you'd have to create a separate thread to run your message loop, something like I do with MessageLoopApartment here.
That said, in a typical WinForms or WPF application you should almost never need a secondary UI thread.

Kill async task c#, wpf

In my application i have class PrimaryViewModel (abstract) and every ViewModel inherit from it.
To get data every X seconds i have written following virtual method
protected virtual async void RunSynchronizeTask()
{
await Task.Run(() =>
{
while (true)
{
Thread.Sleep(RefreshTime);
if (DateTime.Now.TimeOfDay - LastSyncrhonization.TimeOfDay > RefreshTime)
{
Application.Current.Dispatcher.Invoke(delegate
{
GetDataAndRefreshUI();
});
LastSyncrhonization = DateTime.Now;
}
}
});
}
So every viewModel has it owns method and it own task which will get data from DB and refresh binded controls (for example messageViewModel refresh messages list every 0.5 seconds)
but problem is that when i run released application (no debug mode, just build as release and open exe) and close, the app will close, but this async task is still working (i see it in task manager)
What should i do?
How to assign this task to something, i cannot do :
var task = await.Task.Run(...)
If i could i would create some static list with referencses to theses tasks and on application close i would kill them or sth...
You go from an async void to a Task.Run() , which is doubling up.
And then you do the actual work in a Dispatcher.Invoke()
The only thing that is truly async (concurrent) here is the Thread.Sleep().
So you can replace all this with a DispatcherTimer. That should cure your halting problem too.
Thanks Fabian!
works great with cancellation token :
protected virtual async void RunSynchronizeTask()
{
var cancelationToken = new CancellationToken();
App.TaskToDisposeTokens.Add(cancelationToken);
await Task.Run(() =>
{
try {
while (true)
{
Thread.Sleep(RefreshTime);
if (DateTime.Now.TimeOfDay - LastSyncrhonization.TimeOfDay > RefreshTime)
{
Application.Current.Dispatcher.Invoke(delegate
{
GetDataAndRefreshUI();
});
LastSyncrhonization = DateTime.Now;
}
}
}
catch(OperationCanceledException) { }
}, cancelationToken);
}
App.Cs
private void Application_Exit(object sender, ExitEventArgs e)
{
foreach(var token in TaskToDisposeTokens)
{
token.ThrowIfCancellationRequested();
}
}
and in App.Xaml
<Application x:Class="RandevouWpfClient.App"
...
Exit="Application_Exit">

UI doesn't update until async method finishes

In my project I want to show a progress indicator, retrieve the data from the webservice and hide the progress indicator. If I do this I currently have to wait until I retrieved the data from the webservice and then the UI with the results is immediately updated. But the progress indicator never appears. So it seems, that I'm working or blocking on the UI thread. But where?
This is a simple version of my code:
SomePage
private async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
await ShowDetailView();
}
private async Task ShowDetailView()
{
var view = new MyView();
this.detailView = view;
await view.LoadContent();
}
SomeView
public async Task LoadContent()
{
var success = await LoadData();
if (success)
{
ShowInformation();
}
}
public async Task<bool> LoadData()
{
try
{
ShowLoadingProcess();
await Task.Delay(5000);
this.itemList = await WebService.Instance.GetData();
return true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return false;
}
finally
{
HideLoadingProcess();
}
}
private void ShowInformation()
{
Device.BeginInvokeOnMainThread(() =>
{
this.grid.Clear();
foreach(var item in this.itemList)
{
GridItem contentItem = new GridItem(item);
this.grid.Children.Add(contentItem);
}
}
}
private void ShowLoadingProcess()
{
Device.BeginInvokeOnMainThread(() => BringProgressIndicatorToFront());
}
private void HideLoadingProcess()
{
Device.BeginInvokeOnMainThread(() => BringProgressIndicatorToBack());
}
I tried different things, where I got out of sync between the UI and the background thread. E.g. ShowInformation() was called, before LoadData() finished.
Can someone give me a hint about what's wrong here?
I'm not fully sure why your code acts the way it does but you should take into account that Device.BeginInvokeOnMainThread() doesn't give any promises about when the action is executed. Internally it does the following:
// Simplified
s_handler = new Handler(Looper.MainLooper);
s_handler.Post(action);
It passes your action to the message queue which then executes the action at an unknown point of time in the future.
However, what I would suggest you is to rethink the architecture of your application. By taking advantage of the MVVM pattern and data binding you would be able to control the visibility of the progress indicator very easily.
In the ViewModel you would have the following:
private bool isBusy;
public bool IsBusy() {
get { return isBusy; }
set { SetProperty(ref isBusy, value); }
}
In the View that has the above ViewModel as its BindingContext, you would have something like the following:
<ActivityIndicator IsRunning="{Binding IsBusy}" IsVisible="{Binding IsBusy}" />
Now, whenever you'd start a long running operation, you'd simply change isBusy to true and the View would automatically show the running ActivityIndicator due to the binding.
I'm not familiar with xamarin.forms, so the following could be utterly rubbish. If so, please do tell me, I'll delete the answer.
To me it seems you have some Form with an event handler OnItemSelected that should Show a DetailView, which is some other form. After creation of the DetailView object you want to load the detail view. This loading takes some time, and in the mean time you want to inform the operator that the form is loading.
I'm not sure where you want the visual feedback: on SomePage or on SomeView. The answer does not really matter, except that the command to show and hide the visual feedback is best written in the class that shows the feedback.
Show the feedback on SomeView
In this case SomeView has something to show and hide the visual feedback, like a progress bar, or an ajax loader. Code would be like:
As I am familiar with Forms, I'll write it as a Form. Your code will be very similar
class SomeView : Form
{
void ShowVisualFeedBack()
{
...
}
void HideVisualFeedBack()
{
...
}
public async Task LoadDataAsync()
{
bool result = false;
this.ShowVisualFeedBack()
try
{
this.itemList = await WebService.Instance.GetData();
result = true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
this.HideVisualFeedBack();
return result;
}
}
Or if you really need to wait 5 seconds before you start getting the data:
try
{
await Task.Delay(TimeSpan.FromSeconds(5));
this.itemList = await WebService.Instance.GetData();
result = true;
}
Show the feedback on SomePage
class SomePage : Form
{
void ShowVisualFeedBack()
{
...
}
void HideVisualFeedBack()
{
...
}
private async Task ShowDetailView()
{
this.ShowVisualFeedBack();
this.DetailView = new MyView();
await view.LoadContent();
this.HideVisualFeedBack();
}
}
By the way, while your thread is awaiting GetData(), it can't update your visual feedback. Some visual feedback methods like GIF don't need updates by your thread, but if you have something like a progress bar, your thread needs to update it. You can do this by awaiting a maximum time until GetData is completed, update the progress, and await again
var taskWaitASec = Task.Delay(TimeSpan.FromSeconds(1));
var taskGetData = WebService.Instance.GetData();
// note: you are not awaiting yet, so you program continues:
while (!taskGetData.IsCompleted)
{
var myTasks = new Task[] {taskWaitASec, taskGetData}
var completedTask = await Task.WhenAny(myTasks);
if (completedTask == taskWaitASec)
{
UpdateProgress();
taskWaitASec = Task.Delay(TimeSpan.FromSeconds(1));
}
}
Very silly mistake. I had something like this:
private async Task ShowDetailView()
{
var view = new MyView();
this.detailView = view;
await view.LoadContent();
this.mainGrid.Children.Add(this.detailView);
}
Of course he waits until the operation finishes and then the results are shown. This is the correct way:
private async Task ShowDetailView()
{
var view = new MyView();
this.detailView = view;
this.mainGrid.Children.Add(this.detailView);
await view.LoadContent();
}
Should I delete my question? Perhaps, because sometimes you miss the forest for the trees.

Accessing UI controls in Task.Run with async/await on WinForms

I have the following code in a WinForms application with one button and one label:
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void button1_Click(object sender, EventArgs e)
{
await Run();
}
private async Task Run()
{
await Task.Run(async () => {
await File.AppendText("temp.dat").WriteAsync("a");
label1.Text = "test";
});
}
}
}
This is a simplified version of the real application I'm working on. I was under the impression that by using async/await in my Task.Run I could set the label1.Text property. However, when running this code I get the error that I'm not on the UI thread and I can't access the control.
Why can't I access the label control?
When you use Task.Run(), you're saing that you don't want the code to run on the current context, so that's exactly what happens.
But there is no need to use Task.Run() in your code. Correctly written async methods won't block the current thread, so you can use them from the UI thread directly. If you do that, await will make sure the method resumes back on the UI thread.
This means that if you write your code like this, it will work:
private async void button1_Click(object sender, EventArgs e)
{
await Run();
}
private async Task Run()
{
await File.AppendText("temp.dat").WriteAsync("a");
label1.Text = "test";
}
Try this:
replace
label1.Text = "test";
with
SetLabel1Text("test");
and add the following to your class:
private void SetLabel1Text(string text)
{
if (InvokeRequired)
{
Invoke((Action<string>)SetLabel1Text, text);
return;
}
label1.Text = text;
}
The InvokeRequired returns true if you are NOT on the UI thread. The Invoke() method takes the delegate and parameters, switches to the UI thread and then calls the method recursively. You return after the Invoke() call because the method has already been called recursively prior to the Invoke() returning. If you happen to be on the UI thread when the method is called, the InvokeRequired is false and the assignment is performed directly.
Try this
private async Task Run()
{
await Task.Run(async () => {
await File.AppendText("temp.dat").WriteAsync("a");
});
label1.Text = "test";
}
Or
private async Task Run()
{
await File.AppendText("temp.dat").WriteAsync("a");
label1.Text = "test";
}
Or
private async Task Run()
{
var task = Task.Run(async () => {
await File.AppendText("temp.dat").WriteAsync("a");
});
var continuation = task.ContinueWith(antecedent=> label1.Text = "test",TaskScheduler.FromCurrentSynchronizationContext());
await task;//I think await here is redundant
}
async/await doesn't guarantee that it will run in UI thread. await will capture the current SynchronizationContext and continues execution with the captured context once the task completed.
So in your case you have a nested await which is inside Task.Run hence second await will capture the context which is not going to be UiSynchronizationContext because it is being executed by WorkerThread from ThreadPool.
Does this answers your question?
Why do you use Task.Run? that start a new worker thread (cpu bound), and it causes your problem.
you should probably just do that:
private async Task Run()
{
await File.AppendText("temp.dat").WriteAsync("a");
label1.Text = "test";
}
await ensure you will continue on the same context except if you use .ConfigureAwait(false);
Because it's on a different thread and cross-thread calls aren't allowed.
You will need to pass on the "context" to the thread you are starting. See an example here: http://reedcopsey.com/2009/11/17/synchronizing-net-4-tasks-with-the-ui-thread/
I am going to give you my latest answer that I have given for async understanding.
The solution is as you know that when you are calling async method you need to run as a task.
Here is a quick console app code that you can use for your reference, it will make it easy for you to understand the concept.
using System;
using System.Threading;
using System.Threading.Tasks;
public class Program
{
public static void Main()
{
Console.WriteLine("Starting Send Mail Async Task");
Task task = new Task(SendMessage);
task.Start();
Console.WriteLine("Update Database");
UpdateDatabase();
while (true)
{
// dummy wait for background send mail.
if (task.Status == TaskStatus.RanToCompletion)
{
break;
}
}
}
public static async void SendMessage()
{
// Calls to TaskOfTResult_MethodAsync
Task<bool> returnedTaskTResult = MailSenderAsync();
bool result = await returnedTaskTResult;
if (result)
{
UpdateDatabase();
}
Console.WriteLine("Mail Sent!");
}
private static void UpdateDatabase()
{
for (var i = 1; i < 1000; i++) ;
Console.WriteLine("Database Updated!");
}
private static async Task<bool> MailSenderAsync()
{
Console.WriteLine("Send Mail Start.");
for (var i = 1; i < 1000000000; i++) ;
return true;
}
}
Here I am trying to initiate task called send mail. Interim I want to update database, while the background is performing send mail task.
Once the database update has happened, it is waiting for the send mail task to be completed. However, with this approach it is quite clear that I can run task at the background and still proceed with original (main) thread.

Categories