首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用C#运行进程时,如何判断是否出错?

使用C#运行进程时,如何判断是否出错?
EN

Stack Overflow用户
提问于 2011-07-28 04:35:47
回答 2查看 1.5K关注 0票数 0

作为自动化BizTalk部署的一部分,我将使用以下代码从C#运行进程。

代码语言:javascript
复制
        string commandArguments =
            "ImportApp /ApplicationName:\"" + ApplicationFullName +
            "\" /Package:\"" + MsiFilePath+ "\"";
        Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.FileName = "BTSTask.exe";
        p.StartInfo.Arguments = commandArguments;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.CreateNoWindow = true;
        p.Start();
        StreamReader outputStream = p.StandardOutput;
        StreamReader errorStream = p.StandardError;
        // Read the standard output
        //string output = outputStream.ReadToEnd();
        string error = errorStream.ReadToEnd();
        //WriteToLogFile(output);
        if (!error.Equals(""))
        {
            WriteToLogFile(error);
            throw new Exception("Error occurred while importing application " + 
                ApplicationFullName +
                Environment.NewLine +
                error);
        }
        p.WaitForExit();
        p.Close();
        return;

我在日志文件中得到的这个过程的输出如下所示:

代码语言:javascript
复制
Copyright (c) 2010 Microsoft Corporation. All rights reserved.
Information: Importing package "\\tc6218\BizTalkReceiveDrop\servername\Apps\Core.Artifacts.msi" into application "Core.Artifacts" in BizTalk configuration database (server="TC6218", database="BizTalkMgmtDb")...
Information: Performing action "Create" on host "ConfigurationDb" using package "\\ Apps\Core.Artifacts.msi".
Information: Validating resources (count=6)...
* Validating resource (-Type="System.BizTalk:Assembly" -Luid="TC.BizTalk.Functoid.AdvancedDatabaseLookup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f68c7d112162f09d")...
* Validating resource (-Type="System.BizTalk:Assembly" -Luid="TaylorCorp.Erp.ServiceBus.MasterData.Components.BusinessRules, Version=1.0.0.2, Culture=neutral, PublicKeyToken=f68c7d112162f09d")...
* Validating resource (-Type="System.BizTalk:BizTalkAssembly" -Luid="ICSM.Schemas, Version=4.0.1.0, Culture=neutral, PublicKeyToken=69c11018615fef04")...
* Validating resource (-Type="System.BizTalk:BizTalkAssembly" -Luid="ICSM.Pipelines, Version=4.0.1.0, Culture=neutral, PublicKeyToken=69c11018615fef04")...
* Validating resource (-Type="System.BizTalk:BizTalkAssembly" -Luid="ICSM.Maps, Version=2.0.0.0, Culture=neutral, PublicKeyToken=69c11018615fef04")...
* Validating resource (-Type="System.BizTalk:BizTalkBinding" -Luid="Application/Core.Artifacts")...
Information: Performing change requests...
Information: Calling BeginTypeChangeRequest for all selected resource types...
PerformingBeginChangeRequest
PerformingBeginChangeRequest
PerformingBeginChangeRequest
Adding resource (-Type="System.BizTalk:Assembly" -Luid="TC.BizTalk.Functoid.AdvancedDatabaseLookup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f68c7d112162f09d") to store.
Information: PerformingEndChangeRequestsWithRollBack
* Performing EndTypeChangeRequest for resource type "System.BizTalk:Assembly".
* Performing EndTypeChangeRequest for resource type "System.BizTalk:BizTalkAssembly".
* Performing EndTypeChangeRequest for resource type "System.BizTalk:BizTalkBinding".
Error: Resource (-Type="System.BizTalk:Assembly" -Luid="TC.BizTalk.Functoid.AdvancedDatabaseLookup, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f68c7d112162f09d") already in store. 
1) Use BTSTask's overwrite flag or 
2) Set redeploy flag to true in BizTalk Project or 
3) Click overwrite all checkbox in Admin MMC  
to update if the resource exists in the specified target application "Core.Artifacts".
Overwrite flag will be ignored if the resource is associated with another application.


Command failed with 1 errors, 0 warnings.

现在,我要做的就是找出进程是否出错。而且,我需要将输出流和错误流都存储到字符串变量中。(我不想解析这个字符串,您现在可能已经知道了。我不希望遇到微软所说的“死锁”问题,在这种情况下,我不完全理解这些问题。

谢谢你的帮忙!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-28 04:41:17

根据:http://msdn.microsoft.com/en-us/library/aa559686(v=bts.20).aspx

如果成功,则BTSTask.exe进程返回0;如果存在问题,则返回非零值。此返回代码将显示在您的p.ExitCode

票数 2
EN

Stack Overflow用户

发布于 2011-07-28 04:40:48

我想到了3种解决方案:

  1. 订阅Process.ErrorDataRecieved事件。
  2. 从进程中获取返回错误码。通常,如果它不是0,则表示出现错误。
  3. 分析您的进程是否在StandartOutput上报告错误,并通过分析您的“关键字”来读取此字符串。

不幸的是,使用单独的过程你会“松散”控制,所以在你的项目架构过程中,你必须在可靠的架构和基于决策的错误之间进行折衷。

希望这能有所帮助。

致以问候。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6850891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档