Using OO component in VB.net

Peter Volz 1,295 Reputation points
2023-08-07T21:05:44.3066667+00:00

Hello all,

I need to use an object oriented component in my VB.net app, according to its help:

Initialize it in the main app form and inherit it to other forms to avoid crashes (SDK is inside the common App garbage collection)
If you are inside any form, wait in your app until the thread is ended.
Make a thread checker inside each form.
You have to delegate (keyword) them and also you have to add them to the form (marshal the events)

My skill level is basic, not sure how to inherit to other forms and wait for thread end, and a thread checker for each form.

Anyone can shortly advise with this?

For example, I define this in main form:

Friend WithEvents MyOOSDK As Blah

How to inherit to other forms?

Thanks in advance :)

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,833 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,206 Reputation points Microsoft External Staff
    2023-08-08T06:38:37.18+00:00

    Hi @Peter Volz ,

    Please check if the following code helps.

    Public Class MainForm
        Private WithEvents MyOOSDK As Blah
    
        Public Sub New()
            InitializeComponent()
            MyOOSDK = New Blah()
        End Sub
    
        ' Event handler for the SDK component's events
        Private Sub MyOOSDK_EventOccurred(sender As Object, e As EventArgs) Handles MyOOSDK.EventOccurred
            If Me.InvokeRequired Then
                Me.Invoke(Sub() MyOOSDK_EventOccurred(sender, e))
            Else
                ' Handle the event on the UI thread
            End If
        End Sub
    
        ' Method to start a new thread
        Private Sub StartThread()
            Dim myThread As New Thread(AddressOf YourThreadMethod)
            myThread.Start()
            myThread.Join() ' Wait for the thread to finish
        End Sub
    End Class
    
    

    Create a base form (let's say BaseForm) that inherits from MainForm. This base form will contain the MyOOSDK object and its associated event handlers.

    Public Class BaseForm
        Inherits MainForm
    
        ' Thread checker method for UI thread
        Private Sub CheckUIThread()
            If Not Me.InvokeRequired Then
                ' Code that should run on the UI thread
            Else
                Throw New InvalidOperationException("Method must be called on the UI thread.")
            End If
        End Sub
    End Class
    
    
    

    Then create other forms that inherit from BaseForm. These forms will have access to the MyOOSDK object and its event handlers as well.

    Public Class OtherForm
        Inherits BaseForm
    
        ' You can use MyOOSDK and its events here
        Private Sub SomeMethod()
            CheckUIThread() ' Ensure we are on the UI thread
    
            ' Code that interacts with UI elements
        End Sub
    End Class
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    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.


3 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

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.