Try to replace the Dim and If with this fragment:
Invoke(Sub()
Dim CurrentBellIngingForm As New BellRingingForm
CurrentBellIngingForm.Show()
End Sub)
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am trying to open or show a new instance of a form from a new thread in VB.NET.
Here is some code that I have come up with:
Thread starting procedure:
Public Sub StartBellScheduleRun()
Dim BS(NumClocks) As Threading.Thread
For A As Integer = 0 To NumClocks - 1
BS(A) = New Threading.Thread(AddressOf RunBellSchedule)
BS(A).Start(A)
Next
End Sub
Procedure in new thread:
Public Sub RunBellSchedule(ClockIndex As Integer)
Do While Exiting = False
If BellScheduleActive = True Then
If EEMCAlertTime(ClockIndex).ToLongTimeString = WorkingEventBellTimes(EEMCEventIndex(ClockIndex)).EventTime.ToLongTimeString And BellRinging = False Then
EventName = CurrentEventBellTimes(EEMCEventIndex(ClockIndex)).EventName
EventMessage = CurrentEventBellTimes(EEMCEventIndex(ClockIndex)).EventMessage
CurrentEventTime = WorkingEventBellTimes(EEMCEventIndex(ClockIndex)).EventTime
Dim CurrentBellIngingForm As New BellRingingForm
If BellRingingForm.InvokeRequired Then
' Dim asyncResult = BellRingingForm.BeginInvoke(New Action(AddressOf RunBellSchedule))
Dim d As New RunBellScheduleCallBack(AddressOf RunBellSchedule)
Invoke(d, New Object() {ClockIndex})
Else
CurrentBellIngingForm.Show()
'frmModal.ShowDialog(frmMain)
End If
' If ScreenSetting >= 1 And ScreenSetting <= Screen.AllScreens.Count - 1 Then ShowNextEventSreen()
End If
End If
Threading.Thread.Sleep(TimeUpdateRestingTime)
Loop
End Sub
This this the correct approach ore method?
Try to replace the Dim and If with this fragment:
Invoke(Sub()
Dim CurrentBellIngingForm As New BellRingingForm
CurrentBellIngingForm.Show()
End Sub)
Hi @Marc Menzel ,
In WinForms, you cannot directly create or interact with UI controls like forms from a thread other than the main UI thread. Therefore, invoking the creation and display of the form on the UI thread is necessary.
Dim CurrentBellIngingForm As New BellRingingForm()
If frmMain.InvokeRequired Then
frmMain.Invoke(New Action(Sub() CurrentBellIngingForm.Show()))
Else
CurrentBellIngingForm.Show()
End If
Best Regards.
Jiachen Li
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.