Open or show a new instance of a form from a new thread in VB.NET

Marc Menzel 61 Reputation points
2024-09-04T01:33:12.8966667+00:00

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?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,834 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,722 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 117K Reputation points
    2024-09-04T03:16:09.9+00:00

    Try to replace the Dim and If with this fragment:

    Invoke(Sub()
             Dim CurrentBellIngingForm As New BellRingingForm
             CurrentBellIngingForm.Show()
           End Sub)
    

  2. Jiachen Li-MSFT 31,166 Reputation points Microsoft Vendor
    2024-09-04T11:01:39.4266667+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.