应用程序退出流程退出事件

时间:2012-01-04 12:41:06

标签: c# winforms events process

我正在制作表单应用程序,它还在不同的线程上运行控制台进程。基本上我需要在应用程序退出后取消阻止按钮。在我创建事件处理程序之前,完成后的进程刚刚停止,但现在,在事件之后应用程序本身被终止。

以下是制作流程的代码:

public void CallConsole()//This Calls the console application
{
    Thread.CurrentThread.IsBackground = true;
    Process p = new Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.FileName = filename;
    if (checkBox1.Checked)
       p.StartInfo.CreateNoWindow = true;
    p.EnableRaisingEvents = true;
    p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
    p.ErrorDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
    p.Exited += new EventHandler(p_Exited);
    p.Disposed += new EventHandler(p_Exited);
    p.Start();
    p.BeginErrorReadLine();
    p.BeginOutputReadLine();
}

我尝试使用Thread.IsBackground属性,但这并未改变任何内容

这是事件处理程序本身:

void p_Exited(object sender, EventArgs e)//Process on exit or disposed will make button1 avalable
{
    button1.Enabled = true;
}

添加

后应用程序的原因
p.EnableRaisingEvents = true;

现在被杀了,而不仅仅是过程?

2 个答案:

答案 0 :(得分:2)

这里的问题是

void p_Exited(object sender, EventArgs e)//Process on exit or disposed will make button1 available
{
    button1.Enabled = true;
}

需要调用并且没有任何类型的错误处理。一旦我添加了另一个检查button1.InvokeRequired的函数,如果它通过调用再次调用自身,那就很好了

答案 1 :(得分:1)

这里的问题是<access origin="*" /> <allow-intent href="http://*/*" /> <allow-intent href="https://*/*" /> <allow-intent href="tel:*" /> <allow-intent href="sms:*" /> <allow-intent href="mailto:*" /> <allow-intent href="geo:*" /> <feature name="SplashScreen" > <param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen" /> <param name="onload" value="true" /> </feature> 事件正在线程池线程上触发。控件只能在UI线程上修改。

你可以调用Exited,但是通过设置来配置BeginInvoke对象以调用自身更简单:

Process

p.SynchronizingObject = button1; 实现ButtonISynchronizeInvoke对象用于调用其事件。