I have a problem with 'Threading'. I want to have two 'form' that work in separate process, for example if i open a 'OpenFileDialog' from one 'form', the other 'form' works separately and do some things else. (see my code, I'm surry for my bad description.)
I used a 'Thread' and it's work fine. but if my 'form' has 'ContextMenuStrip' control, error 'cross-thread operation not valid' occurred. Please help me.
thanks.
================================================================================= FormMain:
Public Class FormMain
Private Sub cmdShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShow.Click
Form2.ShowMe()
End Sub
End Class
================================================================================= Form2:
Public Class Form2
'Me.ContextMenuStrip1 = New System.Windows.Forms.ContextMenuStrip(Me.components)
'Me.Panel1 = New System.Windows.Forms.Panel
'Me.Panel1.ContextMenuStrip = Me.ContextMenuStrip1
Public Shared Thread_2 As System.Threading.Thread
Public MyDefaultWindowState = FormWindowState.Normal
Private Delegate Sub dlgShowMe()
Public Sub ShowMe()
If Thread_2 IsNot Nothing AndAlso Thread_2.ThreadState = Threading.ThreadState.Running Then
If Me.InvokeRequired Then
Dim d As New dlgShowMe(AddressOf Me.ShowMe)
Me.Invoke(d)
Else
Show_Activate_()
End If
Else
Thread_2 = New System.Threading.Thread(AddressOf Me.Show_View_)
Thread_2.SetApartmentState(System.Threading.ApartmentState.STA)
Thread_2.IsBackground = False
Thread_2.Start()
End If
End Sub
Private Sub Show_Activate_()
Try
Me.Enabled = True
Me.ShowInTaskbar = True
Me.WindowState = Me.MyDefaultWindowState
Me.BringToFront()
Me.Activate()
Catch ex As Exception
MsgBox(ex.Message, , "Show_Activate_")
End Try
End Sub
Private Delegate Sub dlgShow_View_()
Private Sub Show_View_()
Me.Enabled = True
Me.ShowInTaskbar = True
Try
Me.ShowDialog()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical,"Show_View_")
End Try
End Sub
Private Sub cmdOpenFileDialog1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpenFileDialog1.Click
OpenFileDialog1.ShowDialog()
End Sub
End Class
================================================
here is my source code: http://www.mediafire.com/?m8e8i51rr51a35i [63KB]
Run 'FormMain'. click 'cmdShow'. when 'Form2' shown. right click on 'Panel1'. 'ContextMenuStrip1' will be appeared.
close 'Form2.
click 'cmdShow' again. when 'Form2' shown. right click on 'Panel1' again. but you can see error...