2 using System
.Collections
;
3 using System
.Threading
;
11 private IPlugin Plugin
;
14 private Exception Error
;
15 private Engine2 engine
;
16 private bool finished
= false;
18 private void ThreadedWork()
32 public EngineThread(Engine2 engine
, Block b
)
38 MainWindow
.RedrawSchematic();
42 if(b
.Plugin
.NumIn
!= 0)
44 CommSocket cs
= new CommSocket(b
.Plugin
.NumIn
);
46 for(int i
=0; i
<b
.Plugin
.NumIn
; i
++)
48 Socket other
= b
.SocketIn
[i
].Other
;
50 if(other
.Parent
.Plugin
.Out
== null)
54 throw new PluginException(Catalog
.GetString("Plugin has no data on output sockets."));
57 cs
[i
] = other
.Parent
.Plugin
.Out
[other
.Num
];
64 t
= new Thread(ThreadedWork
);
72 MainWindow
.RedrawSchematic();
73 new PluginError(e
, b
, false);
87 MainWindow
.RedrawSchematic();
107 MainWindow
.RedrawSchematic();
115 MainWindow
.RedrawSchematic();
116 new PluginError(e
, b
, false);
127 private static int ConfigThreads
= Config
.Get("engine/threads", 1);
128 private static bool ConfigProgress
= Config
.Get("block/progress", true);
130 private ArrayList Threads
= new ArrayList();
131 private bool stop
= false;
132 private bool running
= false;
133 private FinishCallback finish
;
134 private Progress progress
;
135 private DateTime start
, end
;
137 public delegate void FinishCallback();
138 public delegate void Progress();
140 public Engine2(Schematic s
, FinishCallback finish
, Progress progress
)
143 this.finish
= finish
;
144 this.progress
= progress
;
157 // sprawdzenie, czy jakiś wątek nie skończył pracy
158 for(int i
=Threads
.Count
-1; i
>=0; i
--)
159 if(((EngineThread
)Threads
[i
]).Finished
)
161 // w przypadku błędu praca jest skończona
173 // jeżeli są jakieś wolne sloty na wątki, to trzeba spróbować je wypełnić
174 if(Threads
.Count
< ConfigThreads
)
176 ArrayList Blocks
= s
.Blocks
;
178 for(int i
=0; i
<Blocks
.Count
; i
++)
180 // sprawdzenie czy są wolne wątki
181 if(Threads
.Count
>= ConfigThreads
)
184 Block b
= (Block
)Blocks
[i
];
186 if(b
.CheckState() == Block
.State
.Ready
&& b
.Working
!= true)
187 Threads
.Add(new EngineThread(this, b
));
191 // sprawdzenie czy zostały jakieś wątki
192 if(Threads
.Count
== 0)
203 MainWindow
.RedrawSchematic();
210 // nie powinno nigdy się zdarzyć
212 throw new Exception(Catalog
.GetString("Engine is already running."));
214 start
= DateTime
.Now
;
216 ArrayList Blocks
= s
.Blocks
;
221 // przejrzenie wszystkich bloków i dodanie gotowych do pracy do listy wątków
222 for(int i
=0; i
<Blocks
.Count
; i
++)
224 // sprawdzenie czy są wolne wątki
225 if(Threads
.Count
>= ConfigThreads
)
228 Block b
= (Block
)Blocks
[i
];
230 if(b
.CheckState() == Block
.State
.Ready
)
231 Threads
.Add(new EngineThread(this, b
));
234 // sprawdzanie stanu wątków co 50 ms
235 GLib
.Timeout
.Add(50, new GLib
.TimeoutHandler(Tick
));
238 // zatrzymuje wszystkie wątki
243 foreach(EngineThread t
in Threads
)
246 Threads
.RemoveRange(0, Threads
.Count
);
249 public static void CheckGConf()
251 ConfigThreads
= Config
.Get("engine/threads", 1);
252 ConfigProgress
= Config
.Get("block/progress", true);
257 get { return running; }
260 public string ElapsedTime
264 TimeSpan ts
= end
- start
;
269 return String
.Format("{0:00}.{1}", ts
.Seconds
, ts
.Milliseconds
);
271 return String
.Format("{0:00}:{1:00}.{2}", ts
.Minutes
, ts
.Seconds
, ts
.Milliseconds
);
273 return String
.Format("{0:00}:{1:00}:{2:00}.{3}", ts
.Hours
, ts
.Minutes
, ts
.Seconds
, ts
.Milliseconds
);
275 return String
.Format("{0}.{1:00}:{2:00}:{3:00}.{4}", ts
.Days
, ts
.Hours
, ts
.Minutes
, ts
.Seconds
, ts
.Milliseconds
);