wpf应用程序和外部进程

时间:2015-08-10 13:59:31

标签: c# .net wpf multithreading

我已经编写了一个wpf应用程序,我想从中调用20个外部进程,所有进程完成后,wpf app应该继续正常执行。 我成功地调用了这些流程并且工作得很好。他们从wpf接收参数,但之后没有显示wpf gui,并且在控制台中显示了qouted消息。

当我从下面的代码中调用一次或两次代码时,它运行良好,但几次后停止工作。我已经尝试了注释行

 string arguments = "thumbnail " + input + " " + output.LocalPath;

 Process p = Process.Start(videoToolPath, arguments);
 while(!p.HasExited)
 {
       Thread.Sleep(1000);
 }
 //p.Dispose();
 //p.Kill();
 //p.Close();

控制台:

'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\12.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\12.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\12.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'E:\Projects\MediaGUI\newVSProject\VJProject\VJProject\bin\Debug\VJProject.vshost.exe'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xaml\v4.0_4.0.0.0__b77a5c561934e089\System.Xaml.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\PresentationCore\v4.0_4.0.0.0__31bf3856ad364e35\PresentationCore.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread 0x1ee4 has exited with code 259 (0x103).
The thread 0x1e18 has exited with code 259 (0x103).
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'E:\Projects\MediaGUI\newVSProject\VJProject\VJProject\bin\Debug\VJProject.exe'. Symbols loaded.
Step into: Stepping over non-user code 'VJProject.App..ctor'
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Step into: Stepping over non-user code 'VJProject.App.Main'
Step into: Stepping over non-user code 'VJProject.App.InitializeComponent'
The thread 0x26d8 has exited with code 259 (0x103).
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework.Aero2\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.Aero2.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemXml\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemXml.dll'. Cannot find or open the PDB file.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemXmlLinq\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemXmlLinq.dll'. Cannot find or open the PDB file.
'VJProject.vshost.exe' (CLR v4.0.30319: VJProject.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\UIAutomationTypes\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationTypes.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

2 个答案:

答案 0 :(得分:1)

如果您只想等待外部程序退出:

var process = Process.Start(...);
process.WaitForExit();

int code = process.ExitCode;
if (code != 0)
{
   //failure
}
else
{
   //success
}

(不要在UI-Thread中执行此操作,它会阻止它)

答案 1 :(得分:1)

您想要异步执行此操作。最简单的方法是使用async await模式。坏消息是Process不支持Task async。

public Task ProcessVideo(string input, xxx output)
{
    string arguments = "thumbnail " + input + " " + output.LocalPath;

    Process p = Process.Start(videoToolPath, arguments);
    var tcs = new TaskCompletionSource<object>();
    p.Exited += (o, e) => 
    {
        if(p.ExitCode == 0)
        {
            tcs.SetResult(null);
        }
        else
        {
            tcs.SetException(someException);
        }
        p.Dispose();
    }
    return tcs.Task;
}

//Then call it by 

var listOfFilesToProcess = new List<string>(){ ... };
await Task.WhenAll(listOfFilesToProcess.Select(file => ProcessVideo(file, output));