import dfl.all; import dfl.internal.winapi; import std.cstream; import core.thread; class MyForm : Form { private shared(bool)* start; private shared(bool)* restart; private shared(bool)* end; this(shared(bool)* start, shared(bool)* restart, shared(bool)* end) { this.start = start; this.restart = restart; this.end = end; click ~= &onClick; doubleClick ~= &onDoubleClick; } private void onClick(Object, EventArgs) { if (*start) { //まだcollectorが仕事を終えていないのでキャンセルして最初からやってもらう *restart = true; } else { *start = true; // collectorに仕事をさせる dout.writefln("click"); dout.flush(); } }; private void onDoubleClick(Object, EventArgs) { *end = true; close(); } } void main() { shared bool startFlag; // trueでcollectorが仕事を始める shared bool restartFlag; // trueでcollectorが仕事を最初からやり直す shared bool endFlag; // trueでcollectorを終了する auto f = new MyForm(&startFlag, &restartFlag, &endFlag); auto worker = new Thread( { while(!endFlag) { if(startFlag) { RESTART: dout.writefln("worker"); dout.flush(); for(int i=0; i<100000000; ++i)//重い処理 { if (restartFlag) { f.text = ""; dout.writefln("restart"); dout.flush(); restartFlag = false; goto RESTART; } } f.text = f.text ~ "A"; startFlag = false; } else { Sleep(100); } } }); worker.start(); Application.run(f); }
グローバル変数を止めて,sharedをそれなりに使うようにしてみた.
こんなにたくさんフラグはいらないような気がする.
何か用意されていないのかな.
あと排他制御しなきゃいけないところはどこだろう.