我需要在类之间传递一些变量,我有以下代码片段来解释这种情况。注意以下代码来自 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 读取标准输出。根据输出字符串格式,它会做不同的事情。
我的问题在于代码注释,我想通知 StateMonitor 一旦 flag == true; 做某些事情;我怎么能那样做?
谢谢