patterncsharpModerate
A TaskScheduler that always run tasks in a specific thread
Viewed 0 times
alwaystaskschedulertasksthatthreadspecificrun
Problem
The following is an
When created, a name of the thread was specified. Once you schedule the first task, until it is been
The reason of this class is that sometimes there is a need to guarantee that some tasks must be always scheduled in a specific thread (not the UI thread though). For example, some 3 party dll may have resource leak if you keep creating new threads to call its functions.
```
using Task = System.Threading.Tasks.Task;
using Thread = System.Threading.Thread;
using Barrier = System.Threading.Barrier;
using Monitor = System.Threading.Monitor;
using IDisposable = System.IDisposable;
using TaskEnum = System.Collections.Generic.IEnumerable;
using TaskQueue = System.Collections.Generic.Queue;
using Enumerable = System.Linq.Enumerable;
using ObjectDisposedException = System.ObjectDisposedException;
using _Imported_Extensions_;
namespace _Imported_Extensions_
{
public static class Extensions
{
public static bool Any(this TaskEnum te)
{
return Enumerable.Any(te);
}
public static TaskEnum ToList(this TaskEnum te)
{
return Enumerable.ToList(te);
}
}
}
namespace TaskUtils
{
public class SameThreadTaskScheduler : System.Threading.Tasks.TaskScheduler, IDisposable
{
#region publics
public SameThreadTaskScheduler(string name)
{
scheduledTasks = new TaskQueue();
threadName = name;
}
public override int MaximumConcurrencyLevel { get { return 1; } }
public void Dispose()
{
lock (scheduledTasks)
{
quit = true;
Monitor.PulseAll(scheduledTasks);
}
}
#endregion
#region protected overrides
protected override TaskEnum GetScheduledTasks()
{
lock (schedul
TaskScheduler that always run tasks in a thread it maintains.When created, a name of the thread was specified. Once you schedule the first task, until it is been
Disposeed, a thread will be created and wait for tasks to execute.The reason of this class is that sometimes there is a need to guarantee that some tasks must be always scheduled in a specific thread (not the UI thread though). For example, some 3 party dll may have resource leak if you keep creating new threads to call its functions.
```
using Task = System.Threading.Tasks.Task;
using Thread = System.Threading.Thread;
using Barrier = System.Threading.Barrier;
using Monitor = System.Threading.Monitor;
using IDisposable = System.IDisposable;
using TaskEnum = System.Collections.Generic.IEnumerable;
using TaskQueue = System.Collections.Generic.Queue;
using Enumerable = System.Linq.Enumerable;
using ObjectDisposedException = System.ObjectDisposedException;
using _Imported_Extensions_;
namespace _Imported_Extensions_
{
public static class Extensions
{
public static bool Any(this TaskEnum te)
{
return Enumerable.Any(te);
}
public static TaskEnum ToList(this TaskEnum te)
{
return Enumerable.ToList(te);
}
}
}
namespace TaskUtils
{
public class SameThreadTaskScheduler : System.Threading.Tasks.TaskScheduler, IDisposable
{
#region publics
public SameThreadTaskScheduler(string name)
{
scheduledTasks = new TaskQueue();
threadName = name;
}
public override int MaximumConcurrencyLevel { get { return 1; } }
public void Dispose()
{
lock (scheduledTasks)
{
quit = true;
Monitor.PulseAll(scheduledTasks);
}
}
#endregion
#region protected overrides
protected override TaskEnum GetScheduledTasks()
{
lock (schedul
Solution
Not exactly a review, but since you are asking for simpler way... The simpler way is to run your tasks via dispatcher. Just run it on background thread:
And use
It is a lot simpler than reinventing the wheel. The obvious downside is wpf dependency.
_thread = new Thread(() =>
{
_dispatcher = Dispatcher.CurrentDispatcher;
Dispatcher.Run();
});
_thread.Start();And use
_dispatcher.BeginInvoke and _dispatcher.Invoke to run your tasks on that thread.It is a lot simpler than reinventing the wheel. The obvious downside is wpf dependency.
Code Snippets
_thread = new Thread(() =>
{
_dispatcher = Dispatcher.CurrentDispatcher;
Dispatcher.Run();
});
_thread.Start();Context
StackExchange Code Review Q#43000, answer score: 15
Revisions (0)
No revisions yet.