J
JaedenRuiner
Guest
Okay,
I am missing something that my old school brain of handing threads, background workers, and the like is just not grasping.
Question:
I have a method that i want to run asynchronously, but Visual Studio complains every which way i do it.
async void button1_click(sender, e)
{
this.MyData = await this.LoadData();
}
//This complains about the return type not being a "Task<MyDataObject>", but i can't "create" a "Task<>"
private Task<MyDataObject> LoadData()
{
MyDataObject mdo = new MyDataObject();
this.LoadFromDb(mdo);
return mdo;
}
//This complains that LoadData doesn't "await" anything, so remove the async, but it works
private async Task<MyDataObject> LoadData()
{
MyDataObject mdo = new MyDataObject();
this.LoadFromDb(mdo);
return mdo;
}
That's my issue, straight forward. I understand threads, the CancelationTokens, synchronizing for report progress feed back, all that jazz.
I mean, is it the standard approach to do this:
private Task<MyDataObject> LoadData()
{
MyDataObject mdo = new MyDataObject();
this.LoadFromDb(mdo);
return Task.FromResult(mdo);
}
Are we only supposed to use the "async" if we're going to "await" in the method, and thus if that method does not need to await, we use the "FromResult" statement on Task?
Thanks
Jaeden "Sifo Dyas" al'Raec Ruiner
"Never Trust a computer. Your brain is smarter than any micro-chip."
PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.
Continue reading...
I am missing something that my old school brain of handing threads, background workers, and the like is just not grasping.
- async void X() {}
this is supposed to be for event handlers only in order to "await" a Task in a multi-threaded environment - async Task X() {}
this is for asynchronous methods that don't have a return value - async Task<T> X() {}
this is for asynchronous methods that have a return value - Task X() and Task<T> X()
these are methods that can be called with "await", but can't "await" themselves.
Question:
I have a method that i want to run asynchronously, but Visual Studio complains every which way i do it.
async void button1_click(sender, e)
{
this.MyData = await this.LoadData();
}
//This complains about the return type not being a "Task<MyDataObject>", but i can't "create" a "Task<>"
private Task<MyDataObject> LoadData()
{
MyDataObject mdo = new MyDataObject();
this.LoadFromDb(mdo);
return mdo;
}
//This complains that LoadData doesn't "await" anything, so remove the async, but it works
private async Task<MyDataObject> LoadData()
{
MyDataObject mdo = new MyDataObject();
this.LoadFromDb(mdo);
return mdo;
}
That's my issue, straight forward. I understand threads, the CancelationTokens, synchronizing for report progress feed back, all that jazz.
I mean, is it the standard approach to do this:
private Task<MyDataObject> LoadData()
{
MyDataObject mdo = new MyDataObject();
this.LoadFromDb(mdo);
return Task.FromResult(mdo);
}
Are we only supposed to use the "async" if we're going to "await" in the method, and thus if that method does not need to await, we use the "FromResult" statement on Task?
Thanks
Jaeden "Sifo Dyas" al'Raec Ruiner
"Never Trust a computer. Your brain is smarter than any micro-chip."
PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.
Continue reading...