如果用户停止进程,我怎么能出现错误信息?

时间:2015-10-12 13:17:33

标签: c# visual-studio-2012

我有一种安装程序的方法:

private void Install_Click(object sender, EventArgs e)
{
    using (Process process = new Process())
    {    
        // The installer itself 
        process.StartInfo.FileName = ExeFile;
        if (fileArr1.Equals("installer.ini"))
            process.StartInfo.Arguments = @"-if C:\temp\installer.ini";
        if (fileArr1.Equals("installer_input.txt"))
            process.StartInfo.Arguments = @"-if C:\temp\installer_input.txt";
        process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        process.Start();
        process.WaitForExit();
    }
}

如果用户停止进程,我如何能够显示错误消息:" the process was stop "

2 个答案:

答案 0 :(得分:0)

如果您的流程有名称,您可以按照以下步骤进行检查:

class MyClass
{
    [JsonConverter(typeof(TemplateConverter))]
    public Template Template { get; set; }
}

class Template
{
    /* Your Template class */

    public Type TypeToSerializeInto { get; private set; }
}

public class TemplateConverter : JsonConverter
{
    public override bool CanConvert(Type objectType) 
    { 
        return objectType == typeof(Template);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
        Template val = value as Template;

        writer.WriteStartObject();
        writer.WritePropertyName("Template");

        if (val.TypeToSerializeInto == typeof(Template))
            serializer.Serialize(writer, val);
        else if (val.TypeToSerializeInto == typeof(string))
            serializer.Serialize(writer, val.Name);

        writer.WriteEndObject();
    }
}

答案 1 :(得分:-1)

使用BackgroundWorker并等待,因此用户不会停止进程