main
1using System;
2using System.ComponentModel;
3using Gorilla.Commons.Infrastructure.Logging;
4
5namespace momoney.service.infrastructure.threading
6{
7 public class WorkerThread : Component, IWorkerThread
8 {
9 static readonly object do_work_key = new object();
10 bool is_running;
11 readonly Action background_thread;
12
13 public WorkerThread()
14 {
15 background_thread = worker_thread_start;
16 }
17
18 public event DoWorkEventHandler DoWork
19 {
20 add { Events.AddHandler(do_work_key, value); }
21 remove { Events.RemoveHandler(do_work_key, value); }
22 }
23
24 public void begin()
25 {
26 if (is_running)
27 {
28 throw new InvalidOperationException("Worker Thread Is Already Running");
29 }
30 is_running = true;
31 background_thread.BeginInvoke(null, null);
32 }
33
34 void worker_thread_start()
35 {
36 try
37 {
38 var handler = (DoWorkEventHandler) Events[do_work_key];
39 if (handler != null)
40 {
41 handler(this, new DoWorkEventArgs(null));
42 }
43 }
44 catch (Exception e)
45 {
46 this.log().error(e);
47 }
48 }
49 }
50}