C
C#16mo ago
r2d25551

✅ Async PushButton Functions

What is the proper way to handle FileIO asynchronously? I have a Utility class that performs various functions, one in particular:
public static async Task<string> PickPhoto()
{
return await Task.Run(() =>
{
Task<FileResult> task = MediaPicker.Default.PickPhotoAsync();
if (task != null)
{
FileResult result = task.GetAwaiter().GetResult();
return result.FullPath;
}

return string.Empty;
});
}


protected async void OnPickPhoto(object sender, EventArgs args)
{
await MainThread.InvokeOnMainThreadAsync(() =>
{
if (MediaPicker.Default.IsCaptureSupported)
{
Task<string> task = await Utils.PickPhoto();
if (task != null)
{
string filename = task.GetAwaiter().GetResult();
imageFilename.Source = filename;
}
}
});
}
public static async Task<string> PickPhoto()
{
return await Task.Run(() =>
{
Task<FileResult> task = MediaPicker.Default.PickPhotoAsync();
if (task != null)
{
FileResult result = task.GetAwaiter().GetResult();
return result.FullPath;
}

return string.Empty;
});
}


protected async void OnPickPhoto(object sender, EventArgs args)
{
await MainThread.InvokeOnMainThreadAsync(() =>
{
if (MediaPicker.Default.IsCaptureSupported)
{
Task<string> task = await Utils.PickPhoto();
if (task != null)
{
string filename = task.GetAwaiter().GetResult();
imageFilename.Source = filename;
}
}
});
}
The compiler complains at the await Utils.PickPhoto(); If I remove it, and satisfy the complier errors no result is returned. Is my setup correct? What am I doing wrong? Thank you in advance.
9 Replies
Kouhai
Kouhai16mo ago
You should have
public static async Task<string> PickPhoto() {
FileResult result = await MediaPicker.Default.PickPhotoAsync();
return result.FullPath;
}
public static async Task<string> PickPhoto() {
FileResult result = await MediaPicker.Default.PickPhotoAsync();
return result.FullPath;
}
This assumes result is never null Also
Task<string> task = await Utils.PickPhoto();
if (task != null)
{
string filename = task.GetAwaiter().GetResult();
imageFilename.Source = filename;
}
Task<string> task = await Utils.PickPhoto();
if (task != null)
{
string filename = task.GetAwaiter().GetResult();
imageFilename.Source = filename;
}
Is not right, when you await a PickPhoto the return type is not Task<string> but just string
r2d25551
r2d25551OP16mo ago
Let me give it a try. And I will test for null and return string.Empty if so.
Kouhai
Kouhai16mo ago
You should not GetAwaiter().GetResult() do that at all
r2d25551
r2d25551OP16mo ago
That worked rather well 🙂
Kouhai
Kouhai16mo ago
Awesome! The general rule of thumb, when you see an async method (a method that returns Task) and you need it's result use await it instead of task.GetAwaiter().GetResult();
r2d25551
r2d25551OP16mo ago
To confirm what I have now is:
public static async Task<string> PickPhoto()
{
FileResult task = await MediaPicker.Default.PickPhotoAsync();
if (task != null)
return task.FullPath;
return string.Empty;
}

protected async void OnPickPhoto(object sender, EventArgs args)
{
string filename = await Utils.PickPhoto();
imageFilename.Source = filename;
}
public static async Task<string> PickPhoto()
{
FileResult task = await MediaPicker.Default.PickPhotoAsync();
if (task != null)
return task.FullPath;
return string.Empty;
}

protected async void OnPickPhoto(object sender, EventArgs args)
{
string filename = await Utils.PickPhoto();
imageFilename.Source = filename;
}
What about the UI thread and controls being modified. I do not need any protection?
await MainThread.InvokeOnMainThreadAsync()
await MainThread.InvokeOnMainThreadAsync()
Kouhai
Kouhai16mo ago
You don't need to, when you await the method, it continues on the calling thread which would be the UI thread
r2d25551
r2d25551OP16mo ago
Excellent. Thank you so much again.
Kouhai
Kouhai16mo ago
No problem!
Want results from more Discord servers?
Add your server