Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to load any application from a data file using this code:
VB
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

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?
Posted
Updated 14-Apr-11 12:30pm
v2

1 solution

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
 
Share this answer
 
v2
Comments
Stuart Nathan 15-Apr-11 4:14am    
I looked at the articles and got a bit lost.
A little bit of code would help.
Thanks
Sergey Alexandrovich Kryukov 15-Apr-11 4:33am    
Sure, but now it's your turn. Read again and highlight your questions -- I'll try to answer closer to your specific problem.

First step you need is to replace "Me.WindowState = FormWindowState.Normal" with Invoke or BeginInvoke with delegate executing this line of code. Did you work with delegates?
Look at the sample here: http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx

--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900