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.
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 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
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 5 years ago.
Improve this question
I'm trying to dispatch my thread as
private Thread thread;
. . .
if(thread != null && thread.IsAlive) thread.Abort();
. . .
thread = new Thread(myProcess);
thread.Start();
to start over myProcess from the very beginning, if it is already run.
However, it never ever comes to thread.Abort(), and WPF UI responce time still quite bad.
How could I make it right way?
So, with all conversations in my post, as well as discussions outside of StackOverflow, I found, that I ask about quite fundamental issue, which haven't any solution is the Microsoft Windows architecture:
When external task started in some thread with API, and it is running, there is no way to cancel this execution, and return it to the initial suspend status, but another API (as "Cancel" in ProgressBar) made specially to return it to the Idle Loop waiting.
In other time sharing OS such a special point exists, but not in Windows.
As a result, when no Cancel API interface described, we should run another thread, possibly, from the tread pool to save CPU time in the system, and keep waiting, until unnecessary one would be completed.
Respond from Tekla developers confirm my conclusion:
"Such methods do not exist. As I wrote before, implementing multi-threading in your app could not improve performance changing colors because TeklaStructures internal code does not use it."
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
My question is the following. What is the better way, if I use more Timers with fewer tasks or I need define fewer Timers with more tasks? With which solution can I achive better performance?
Thank you!
In your specific scenario, you need to consider two things
1. Each timer will run in different thread.
2. Do you need more threads as compared to tasks or not?
Best practices can be as follows:
1. Use Quartz scheduler, so that you dont need to set frequency of each timer specially.
2. Define tasks as jobs and schedule them using cron-expressions.
3. Use TPL for async operations. TPL will allow to automatically create as much threads as you need (if your task is heavy). You can also use await-async to marshal your task on separate thread without stopping your main thread.
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.