I want to catch an error on a different form.

Asked 2 years ago, Updated 2 years ago, 73 views

The structure may be a little difficult to understand, but I will let you know the current situation.

STEP1: Call Form 1 from Class 1 (The Form 1 argument is defined on the interface and passes the class1 instance)

STEP 2: Create a new thread and start the method for the interface received with the argument from the Form 1 Down event.

STEP 3: I want to pick up an error in the thread that I started, but it's caught inside the method I ran, but I can't catch it even if I write a try at the top.

Class 1

 dim f1 as new form 1(me)
f1.showdialog()

Form1

 dim IF as Interface
sub new (byval IF as Interface)
   me.InterFace=IF
end sub

' FormShown event
Dim Thread 1 As New System.Threading.Thread (New System.Threading.ThreadStart (AddressOfthreadA))
traffic1.isBackGround=true
traffic1.start()

private subthreadA
   me.IF.method
end sub

The best timing to catch an error is class1, which calls Form1, but I can't catch an error in the first place.
Does anyone know?

.net visual-studio vb.net

2022-09-30 19:29

2 Answers

The best timing to catch errors is class1, which calls Form1, but I can't catch them in the first place.

"It says ""Catch Error"", but as long as the expression ""catch"" is used, it is considered to be intended to catch an exception."
Exceptions are thrown at the caller.However, the thread is not in the calling relationship.In fact, running tread1.start() moves the caller to the next line.Therefore, we cannot catch any exceptions that occurred on another thread.

However, Async introduced from Visual Studio 2012 transfers any exceptions that have been run on a separate thread to the caller.
As the question does not mention the version of Visual Studio and what you would like to do with a different thread, we cannot determine if this Async feature is available, so we will only introduce it.
Note: Asynchronous programming using Async and Await


2022-09-30 19:29

As Sayuri wrote, the exception cannot be caught in Class 1 because the execution thread is different.In such a case, the Control.BeginInvoke method can start processing on the UI thread, so if you re-throw the exception, it will be the desired behavior.

Private SubthreadA()
    Try

        me.IF.method

    Add Catch ex As Exception 'Catch

        ThrowException(ex)

    End Try
End Sub

' Method for Throwing Exceptions on UI Threads
Private Sub ThrowException (ex As Exception)

    Lock for SyncLock Me' flag

        ' It is possible that the form is already closed when an exception occurs, so it is determined.
        If_closed Then

            Return

        End If

        ' If InvokeRequired is True, BeginInvoke as the threads are different
        If Me. InvokeRequired Then

            Me. BeginInvoke (New Action (Of Exception) (AddressOfThrowException), ex)

            Return

        End If

        ' Reslow exception on main thread
        Throw New Exception ("threadA encountered an exception", ex)

    End SyncLock

End Sub

' Form Closed Flag
Private_closed As Boolean

' Flag when the form is closed
Private SubForm1_FormClosed(sender As Object, eAsFormClosedEventArgs)Handles Me.FormClosed

    SyncLock Me

        _closed=True

    End SyncLock

End Sub


2022-09-30 19:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.