WPF应用程序和外部进程

本文关键字:进程 外部 应用程序 WPF | 更新日期: 2023-09-27 18:07:07

我写了一个wpf应用程序,从中我想调用20个外部进程,所有进程完成后,wpf应用程序应该继续正常执行。我成功地调用了这些进程,它们运行得很好。它们从wpf接收参数,但之后wpf gui不显示,并在控制台中显示引用消息。

当我从下面的代码调用process一两次时,它工作得很好,但几次之后它就停止工作了。我已经尝试了注释行

 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.

WPF应用程序和外部进程

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

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

(不要在UI-Thread中这样做,它会阻塞它)

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

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));