Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
To enhance performance, I want to exit a function without waiting for all async functions to complete. I only care about the results of the first function. The others should complete when ever and log their own exceptions.
// I care about this result
int? bookingId = await BookingService.SaveBooking(...);
// These should run async in the background. All async methods.
EmailService.SendEmail(...);
CreditCardService.SaveCard(...);
SMSService.SendSms(...);
return bookingId;
What are the risks of omitting await keyword there?
The main risk here is that they fail and cause unobserved exceptions. A secondary risk is that they attempt to interact with some async state that no longer makes sense once the main operation has completed. If you're happy that neither of these things are a problem, you should be fine - but you should be a little cautious with this approach. This is essentially a "fire and forget" scenario - the compiler will try to fight you
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to wait for an event signal from another thread without blocking the waiting thread. Ideally using await.
I thought of this solution:
await Task.Run(() => myAutoResetEvent.WaitOne());
I would like to know if there is something conceptually wrong with it and if there are better alternatives
Thanks!
What is wrong with doing this?
You're using a thread pool thread just to wait for a "signal".
what is a better alternative?
You can use an async-compatible AutoResetEvent, such as the one in my AsyncEx library or the one on Stephen Toub's blog.
Also, most of the time auto reset events are the wrong thing to use. manual reset events are much more common. I'd recommend re-evaluating this choice of synchronization primitive.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a WCF service which essentially just does this:
public SomeClass SomeMethod(){
using(var db = new LinqToSqlContext()){
return db.SomeMethod();
}
}
Client side it has auto generated asynchronous methods but I'm just wondering if there is any benefit to making the service itself use async Tasks? After all the method finishes when it finishes and it could only ever await on a single method.
Short answer: probably not.
async can help a WCF service scale faster and farther, but if your backend is just a single SQL server (as seems likely from your code), then even synchronous code can probably scale further than your SQL server.
Long answer: you won't know for sure until you do it both ways and run performance tests.
If the method can do multiple tasks independently at the same time, then there might be a performance benefit from using Tasks to run them asynchronously.
Otherwise there's no point (and it may even be a negative effect due to overhead of multi-threading).
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
My application will have to wait a sequence of events from a external system that can take up to 5 minutes to arrive. Is it advisable to use async/await? If it's not, what should you use?
Thanks!
If you start your Task as this, it may be ok. This starts the task as "long running" and the TPL will probably schedule it on a different CPU and will not use a ThreadPool thread:
void RunLongTask()
{
// long work to do
// in case of wpf you can report progress to ui
Dispatcher.Invoke(ProgressDelegate, 0);
// more work
Dispatcher.Invoke(ProgressDelegate, 1);
// etc...
}
async Task RunLongTaskAsync()
{
await Task.Factory.StartNew(RunLongTask, TaskCreationOptions.LongRunning);
}
But those questions always depend on the overall work load for your application and the machine it is running on. If you have enough resources, who cares about one long running task. But if your machine is small and you want to do a lot of other stuff... take care.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I was asked at an interview today about how I would implement my own Thread. As a hint I was told that implmenting Runnable was one thing to consider, and what are the others?
I was completely stumped but even after researching it online, I still have no idea what the answer is or if it was even a valid question. I'm leaning towards the latter.
So my question is:
What things would you need to consider if you wanted to write your own implementation of the Thread class in C#?
First up, Runnable probably refers to Action. Action is a delegate type used to store a reference to a method. You use this to tell a thread what method to start on.
I expect they only wanted you to illustrate how to;
start a thread with a delegate (that is how Runnable enters the fray).
-or-
schedule work to be performed on a worker thread. I would use a call to Task.Run(new Action(....)) for this. It is concise and modern.
I would always do the latter until instructed to detail the lower level Thread class.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm a contractor, I've taken over a bunch of projects written by a developer who has left. One of the ASPX pages is using Tasks with a long chain of continuations to launch long processing.
When I set breakpoints in a continuation they are never hit unless I call Wait() on the Task, so I assume the Task is never being executed. I can't call Start() on a continuation and I have no access to the creation of the original Task because it occurs in a 3rd party library. How can I start the Task asynchronously?
A continuation Task starts only after the antecedent Task completes (that is the whole point of continuation). And there is no alternative way to start it, assuming you can't modify the code that creates the continuation.