使用流程执行应用时出错 [英] Error using Process to execute app

查看:112
本文介绍了使用流程执行应用时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下代码从数据文件加载任何应用程序:

I want to load any application from a data file using this code:

Private Sub LoadApp(ByVal Path As String)
   Dim ERROR_FILE_NOT_FOUND As Integer = 2
   Dim ERROR_ACCESS_DENIED As Integer = 5
   Try
      Dim omProcess = New Process
      WindowState = FormWindowState.Minimized
      Application.DoEvents()
      With omProcess
         AddHandler .Exited, AddressOf omProcess_Exited
         .EnableRaisingEvents = True
         .StartInfo.FileName = Path
         .Start()
      End With
   Catch e As Win32Exception
      If e.NativeErrorCode = ERROR_FILE_NOT_FOUND Then
         ShowMsg(My.Resources.strMissingFile, e.Message, True)
      ElseIf e.NativeErrorCode = ERROR_ACCESS_DENIED Then
         ShowMsg(e.Message & ". You do not have permission to access this file.")
      Else
         ShowMsg(e.Message)
      End If
   End Try
End Sub
Private Sub omProcess_Exited(ByVal sender As Object, ByVal e As System.EventArgs)
   Me.WindowState = FormWindowState.Normal
End Sub


该应用会加载并正常运行,但是当您关闭该应用时,
它在omProcess_Exited中失败,并显示以下错误:

未处理System.InvalidOperationException
Message =跨线程操作无效:从创建该线程的线程以外的线程访问控件"Manager"."

有任何想法吗?


The app loads and performs properly, but when you close the app,
it fails in omProcess_Exited with this error:

System.InvalidOperationException was unhandled
Message="Cross-thread operation not valid: Control ''Manager'' accessed from a thread other than the thread it was created on."

Any ideas?

推荐答案

这里有一些错误,都是关于线程的.

例外情况也与线程有关.事件ProcessExited的处理程序是从UI线程以外的线程调用的.您不能从此线程调用任何UI属性或方法.而是应使用DispatcherControl的方法InvokeBeginInvoke.使用Control的哪个实例都没有关系.例如,它可能是您的表格.如果不确定UI线程中是否正在运行某些代码,则可以通过Control.InvokeRequired检查.
您可以在这里找到有关如何使用调用及其工作方式的全部信息:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [
There are some mistakes here, all about threading.

The situation with exception is also related to threads. The handler of the event ProcessExited was called from the thread other than UI thread. You cannot call any UI properties or methods from this thread. Instead, you should use the method Invoke or BeginInvoke of Dispatcher or Control. It does not matter what instance of Control to use; it could be your form, for example. If you are not sure if some code is running in UI thread or not, you can check if via Control.InvokeRequired.
You can find all on how to use invocation and how it works here:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

As a rule of thumb, never use DoEvents. This is a common mistake. If you want to do some processing which takes time, don''t do in in the UI thread, use a special thread for this purpose.

See my past answers on how to use threading with Forms. This is a collection of references to my past Answers, good enough to cover your topics:
How to get a keydown event to operate on a different thread in vb.net[^].

—SA


这篇关于使用流程执行应用时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆