我需要在类之间传递一些变量,我有一个以下代码片段来解释这种情况。请注意以下代码来自app1,SimpleScene()类是入口点。
class SimpleScene {
bool isReady;
bool result;
Protected override Run() {
// instance of class StateMonitor
StateMonitor sm = new StateMonitor;
Listener(sm);
sm.proc();
while(!isReady) {}
result = func();
if (result) {
// need to inform StateMonitor to do a certain action, e.g. set bool flag = true, and StateMonitor does something iff flag == true;
}
}
void OnReady(bool isOnReady) {
if(isOnReady)
isReady = true;
}
Private void Listener(StateMonitor sm) {
sm.OnReady += new StateMonitor.ready(OnReady);
}
bool func() {
//do something...
}
}
class StateMonitor {
public delegates void ready(bool isReady);
public event ready OnReady;
// start app2 as a new process
public void proc() {
Process p_app2 = new Process();
// omit other startinfo for app2.
p_app2.OutputDataReceived += new DataReceivedEventHandler(outputHandler);
p_app2.Start();
p_app2.BeginOutputReadLine();
}
public void outputHandler(object sender, DataReceivedEventArgs line) {
// omit output data line match
if (OnReady != null)
OnReady(true);
}
}
类StateMonitor启动另一个应用程序 - 让我们说app2 - 并从app2读取stdout。根据输出字符串格式,它将执行不同的操作。
我的问题在于代码注释,我想在State = = true时通知StateMonitor做某些事情;我怎么能这样做?
谢谢
答案 0 :(得分:0)
您将要使用线程。你将在StateMonitor中触发一个基于布尔循环的线程。然后,当您想要在该标志为真时执行某些操作时,从SimpleScene设置该布尔值。那里有很多例子。点击这里:
http://msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.90).aspx
Worker = StateMonitor
WorkerThreadExample = SimpleScene