【发布时间】:2013-10-13 14:27:04
【问题描述】:
所以我正在尝试制作一个启动 3rd 方 exe 来执行一些文件操作的应用程序, 基于文件名列表。 因此,如果列表有 13 个项目,我将循环 13 次,每次启动外部进程,通知用户现在正在处理哪个文件,启动进程并等待它退出。为了通知用户,另一个列表框用作喊话框。问题是, .waitforexit() 以某种奇怪的方式冻结了整个线程,因此外部程序被正常调用,文件被正常处理,但主窗口被冻结,直到所有项目都完成。所以基本上 Shoutbox 被冻结并且只有在整个循环完成后才会收到所有信息的垃圾邮件。我尝试了很多方法来实现这一点,例如启动新线程、使用线程池、计时器等等。任何帮助表示赞赏。 代码:
Imports System.Windows.Threading
Imports System.Windows.Forms
Imports System.IO
Imports System.Threading
If Listbox2.Items.Count > 0 Then
tabctrl.SelectedIndex = 2
Listbox3.Items.Add(DateTime.Now.ToString & ": Process initiated.")
For i = 0 To Listbox2.Items.Count - 1
Listbox3.Items.Add(DateTime.Now.ToString & ": Processing :" & Listbox1.Items.Item(i))
If System.IO.File.Exists(Listbox2.Items.Item(i)) = False Then
Dim pInfo As New ProcessStartInfo()
With pInfo
.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
.FileName = System.IO.Directory.GetCurrentDirectory & "\" & "myapp.exe"
.argouments = "w/e"
End With
Dim p As Process = Process.Start(pInfo)
p.WaitForExit()
p.Dispose()
Else
Listbox3.Items.Add(DateTime.Now.ToString & ":! " & Listbox2.Items.Item(i) & " already exists. Moving to next file..")
End If
Next
Listbox3.Items.Add("*-*")
Listbox3.Items.Add(DateTime.Now.ToString & ": Done.")
End If
【问题讨论】:
-
WaitForExit阻塞线程。但在进程之后/之前,您可以添加Application.DoEvents(),以便主线程处理其事件。 -
如果一旦你启动 Application.DoEvents 路径,它将永远主宰你的命运,吞噬你,它将...
-
因为我忘了提到这是 WPF Application.DoEvents() 至少用这种语法是不可能的..
标签: vb.net process waitforexit