summaryrefslogtreecommitdiff
path: root/product/Service/Infrastructure/Threading/BackgroundThread.cs
blob: 21e9645f9fbd9c06c0b218edc3c7578f053cfc65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using MoMoney.Utility.Core;

namespace MoMoney.Service.Infrastructure.Threading
{
    public interface IBackgroundThread : IDisposableCommand
    {
    }

    public class BackgroundThread : IBackgroundThread
    {
        readonly IWorkerThread worker_thread;

        public BackgroundThread(IDisposableCommand command_to_execute) : this(command_to_execute, new WorkerThread())
        {
        }

        public BackgroundThread(IDisposableCommand command_to_execute, IWorkerThread worker_thread)
        {
            this.worker_thread = worker_thread;
            worker_thread.DoWork += (sender, e) => command_to_execute.run();
            worker_thread.Disposed += (sender, e) => command_to_execute.Dispose();
        }

        public void run()
        {
            worker_thread.begin();
        }

        public void Dispose()
        {
            worker_thread.Dispose();
        }
    }
}