comparison defical-sharp/defical-gui/threader.cs @ 0:ebed2bd0d300

Initial import from svn. History be damned.
author Edho P. Arief <me@myconan.net>
date Fri, 02 Apr 2010 23:11:57 +0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ebed2bd0d300
1 using System.Threading;
2
3 namespace defical_gui
4 {
5 public partial class main
6 {
7 private void go()
8 {
9 this.isProcessing = true;
10 int numThreads = (System.Environment.ProcessorCount * 3)/2;
11 Thread[] threads = new Thread[numThreads];
12 int start;
13 int end = 0;
14 txtResult.Dispatcher.BeginInvoke(new StringDelegate(printMessage), new object[] { 200, "" });
15 for (int i = 0; i < numThreads; i++)
16 {
17 threads[i] = new Thread(goBT);
18 threads[i].IsBackground = true;
19 start = end + 1;
20 end = start + ((this.numVerMain + this.numVerDef + 1) / 2) / numThreads;
21 if (i == numThreads - 1) { end = (this.numVerMain + this.numVerDef + 1) / 2; }
22 threads[i].Start(new int[] { start, end, i });
23 }
24 while (this.isProcessing)
25 {
26 Thread.Sleep(100);
27 if (!this.isProcessing) { break; }
28 bool threadIsProcessing = true;
29 switch (this.isFindEverything)
30 {
31 case true:
32 {
33 threadIsProcessing = false;
34 for (int i = 0; i < numThreads; i++)
35 {
36 if (threads[i].IsAlive)
37 {
38 threadIsProcessing = true;
39 break;
40 }
41 }
42 break;
43 }
44 case false:
45 {
46 threadIsProcessing = true;
47 for (int i = 0; i < numThreads; i++)
48 {
49 if (!threads[i].IsAlive)
50 {
51 threadIsProcessing = false;
52 break;
53 }
54 }
55 break;
56 }
57 }
58 this.isProcessing = threadIsProcessing;
59 }
60 txtResult.Dispatcher.BeginInvoke(new StringDelegate(printMessage), new object[] { 201, "" });
61 for (int i = 0; i < numThreads; i++)
62 if (threads[i].IsAlive)
63 threads[i].Abort();
64 }
65 private void goBT(object o)
66 {
67 int[] args = o as int[];
68 int start = args[0];
69 int end = args[1];
70 int threadID = args[2];
71 for (int i = start; i <= end; i++)
72 {
73 if (this.isProcessing)
74 {
75 libbacktrack.Backtrack myWork = new libbacktrack.Backtrack(this.graphType, this.numVerMain, this.numVerDef, i,this.isFindEverything);
76 myWork.Walk();
77 if (this.isProcessing)
78 {
79 if (myWork.IsSemt)
80 {
81 txtResult.Dispatcher.BeginInvoke(new StringDelegate(printMessage), new object[] { 400, "" });
82 txtResult.Dispatcher.BeginInvoke(new StringDelegate(printMessage), new object[] { 0, myWork.Result });
83 if (!this.isFindEverything) { break; }
84 }
85 else { txtResult.Dispatcher.BeginInvoke(new StringDelegate(printMessage), new object[] { 401, i.ToString() }); }
86 }
87 }
88 }
89 }
90 }
91 }