Skip to content

Instantly share code, notes, and snippets.

@martianboy
Created March 16, 2012 10:05
Show Gist options
  • Save martianboy/2049396 to your computer and use it in GitHub Desktop.
Save martianboy/2049396 to your computer and use it in GitHub Desktop.
Threading problem
Public Class Form2
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 'If Thread1.ThreadState = Threading.ThreadState.Stopped Then
' اگر این فرم بسته بود، تِرِد را از نو میسازد و اجرا میکند
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
' اگر خط های زیر را فعال کنید، اصلا فرم نشان داده نمیشود
'If ContextMenuStrip1.InvokeRequired Then
' Dim d As New dlgShow_View_(AddressOf Show_View_)
' ContextMenuStrip1.BeginInvoke(d)
'Else
Me.ShowDialog()
'End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Name & ".Show_View_")
End Try
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'
End Sub
Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
'
End Sub
Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.ShowDialog()
End Sub
End Class
Public Class FormMain
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form2.ShowMe()
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment