Best way to schedule tasks in C# [closed] - c#

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 9 years ago.
Improve this question
I'm developing a C# program that needs to be able to schedule a variety of asynchronous tasks at different times. I need a solution that activates every second to check for tasks to execute, and eats up as few resources as possible. It also needs to scale well to a large number of tasks. Which of these is better to use, or is there any ?
1) Using a System.Timers.Timer object with a 1 second elapsed time to trigger an event that executes tasks, or
2) Using a separate thread that sleeps for 1 second and then executes tasks when it wakes up, or
3) Something else entirely.

System.Threading.Timer is extremely lightweight. You could create a separate timer for each task so that the timer comes due when the task is supposed to execute.
Internally, I believe that the system will keep track of what timer is due next so it only has to monitor one timer at any given time (source/similar question).

Try looking at Quartz.Net for your scheduling needs. I think it's pretty good.

Related

Brute force on several BackgroundWorkers faster? [closed]

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 5 years ago.
Improve this question
My C# Brute Force programm uses 20% of CPU in runtime. Im only using 1 Backgroundworker for it. If I would spread this task into several parts and insert them in 7-8 Backgroundworkers, would that be faster and use more CPU?
The short answer is maybe.
The long answer is that it depends upon multiple factors:
Is your task CPU bound, if it's not the CPU holding up the task then multithreading probably won't help.
How many cores does your processor have and does it have hyperthreading enabled: if it only has one thread, trying to multithread will actually slow it down; if it has more, you can use as many threads in your program as you have available in the OS. (I suggest that you use the Environment.ProcessorCount value to determine the number of threads that you start).
How much cross-thread synchronisation will have to occur. If your threads are spending ages waiting to write into locked shared variables or passing data between them, it will likely slow your application down.
My main suggestion would be to test it!
You can easily time the execution time of a segment of code fairly accurately using the intuitive Stopwatch class.
Finally, you might want to try using a Thread[] rather than background workers, in general, these have lower overheads so are slightly quicker.
I tested the word "stack". One time with only 1 Backgroundworker the second time with 5. Backgroundworker 1 searches for a word with the length of 1. background worker 2 for a word with the length of 2 and so on.
Word: stack
Backgroundworker Time
1 1min 8sec
5 35sec

Is it advisable to use async/await for a long running operation? [closed]

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.

Group Box in different Thread [closed]

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 have 5 Group Box in my software I want to run them differently for example I have one group box which takes lot of time around 20-30 minutes till then my software hangs. SO I want to make other functionality working when a particular group box is working any solution for it?
Group boxes don't "work". If they're just some GUI for a resource intensive task, you probably want to run that task asynchronously (or multi-threaded, depending on your requirements) and only use the GUI for settings / updates / whatever. The key is to never do much work on the GUI thread - if a GUI action takes more than say 50-100ms, it should probably be done elsewhere. Note that using await makes this very easy to do properly - if that's not available, a BackgroundWorker is probably the best option. Do note it doesn't shield you from synchronization issues, though!

More Timers fewer tasks or fewer timers with more tasks [closed]

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.

Will a single task in C# be executed in parallel on a multi-core system? [closed]

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 9 years ago.
Improve this question
I am just finding my way around parallel programming in C# and understood the significance of cores and true parallel programming.
But I still have a question:
Say I have a long running task does that mean this will be executed using threads from thread pool and in different cores for true parallel programming.
Or does it depend on the actual delegate that is passed onto the task?
I Hope my question is clear.
The delegate itself makes no difference. It is the TaskScheduler that matters.
The default TaskScheduler will run them via the ThreadPool.. to have them run synchronously, you would pass in a TaskScheduler instance that is currently being used.. such as the static TaskScheduler.FromCurrentSynchronizationContext.
True parallel programming requires multiple cores since threads must execute on separate threads to truly run in parallel.. In a single core system you can only achieve fake parallelism since different treads must share the core through allocated time slots. Other treads are waiting while the current thread is running on the single core.

Categories