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