Jan 28, 2018 07:32 PM
(This post was last modified: Feb 13, 2018 03:55 AM by Ninja.)
So some people still use the traditional Thread class, and luckily most switched to Tasks which are amazing, but another way to perform concurrent actions is using an AppDomain. The AppDomain will handle the concurrency for you. For a detailed description on what an AppDomain is and how it works, see the links below.
https://msdn.microsoft.com/en-us/library...10%29.aspx
https://stackoverflow.com/a/12233139
I put together a quick example project, and you can find the code below.
Usage:
What it looks like:
![[Image: gOyprRF.png]](https://i.imgur.com/gOyprRF.png)
By unloading the AppDomain in which the Worker classes run, you abort all running actions. This is similar to using CancellationTokens when using tasks. The pro's are that you get all the perks of using AppDomain, like permissions and restrictions (as Barret showed off in the VB.net section), and you can easily stop all threads by unloading the AppDomain instance, and you don't have to manage all threads inside. A common usecase for the AppDomain class is for a plugin system. Though if you don't need any of the features an AppDomain offers, then I suggest just using the Thread Parallel Library (TPL).
https://msdn.microsoft.com/en-us/library...10%29.aspx
https://stackoverflow.com/a/12233139
I put together a quick example project, and you can find the code below.
Code
public class WorkerFactory
{
private AppDomain _workerDomain { get; }
public WorkerFactory()
{
_workerDomain = AppDomain.CreateDomain("WorkerDomain");
}
public T Create<T>() where T : Worker
{
return CreateWorker<T>();
}
public T CreateAndRun<T>() where T : Worker
{
T worker = CreateWorker<T>();
worker.Start();
return worker;
}
protected T CreateWorker<T>() where T : Worker
{
Type type = typeof(T);
return _workerDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName) as T;
}
public void Stop()
{
AppDomain.Unload(_workerDomain);
}
}
public abstract class Worker : MarshalByRefObject
{
public abstract void Start();
}
public sealed class TestWorker : Worker
{
public override async void Start()
{
for (int i = 1; i <= 100; i++)
{
Console.WriteLine("Running from [{0}]. Output: {1}.", AppDomain.CurrentDomain.FriendlyName, i);
await Task.Delay(1000);
}
}
}Usage:
Code
static void Main(string[] args)
{
WorkerFactory factory = new WorkerFactory();
Console.WriteLine("Starting factory. Press any key to stop.");
factory.CreateAndRun<TestWorker>();
Console.ReadKey();
factory.Stop();
Console.WriteLine("Stopped factory. Press any key to exit.");
Console.ReadKey();
}What it looks like:
![[Image: gOyprRF.png]](https://i.imgur.com/gOyprRF.png)
By unloading the AppDomain in which the Worker classes run, you abort all running actions. This is similar to using CancellationTokens when using tasks. The pro's are that you get all the perks of using AppDomain, like permissions and restrictions (as Barret showed off in the VB.net section), and you can easily stop all threads by unloading the AppDomain instance, and you don't have to manage all threads inside. A common usecase for the AppDomain class is for a plugin system. Though if you don't need any of the features an AppDomain offers, then I suggest just using the Thread Parallel Library (TPL).

