locked
What are some useful tips about properly implementing multithreading in a VB.NET application? RRS feed

  • Question

  • What are some useful tips about properly implementing multithreading in a VB.NET application?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, April 10, 2009 8:32 AM

Answers

  • .NET applications can perform multiple tasks simultaneously using multithreading in which individual tasks execute on separate threads. It improves the performance and responsiveness of the applications. Generally, we need to note the following tips when we are multithreading:

     

    ·  Use the BackgroundWorker class. BackgroundWorker is supplied by .NET Framework2.0. It is easy to use for executing an operation on a separate thread. There is a full explanation which includes its functionalities and the code samples in the MSDN document. Read the respective document to understand its usage.

     

    ·  Share data between multiple threads in the application. We need to use the thread synchronization mechanism to avoid corrupting the data.

     

    ·  Modify the property of the form and its controls in the main UI thread from the worker thread. When the code in the worker thread will modify the property of the form and its controls in the main UI thread, we need to use the Control.Invoke(or BeginInvoke) method to marshal the call to the correct main UI thread.

     

    ·  Exception handling. If we don’t handle exceptions in the thread function, the unhandled exception will force the hosting process to exit.

     

    ·  Using asynchronous method invocation. Asynchronous method invocation is a feature supplied by CLR to fork the stream of execution into two branches. CLR implements this mechanism by using a work queue. It will package the method parameters and target method address into one request message and queues this message to a process-wide work queue. CLR maintains one thread pool to process the every request in that queue.

     

    You can use the asynchronous method invocation feature in a .NET application via a delegate object. Take a look at the following code snippet about how to use it.

     

    For more information about asynchronous method invocation, please refer to the following articles:

    1)   .NET Delegates: Making Asynchronous Method Calls in the .NET Environment

    http://msdn.microsoft.com/en-us/magazine/cc301332.aspx

    2)   Asynchronous Method Invocation

    http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx

    Imports System.Runtime.Remoting.Messaging
    
    Public Class Form1
    
        Public Delegate Function operationdelegate(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim obj As New demo1
            Dim num1 As Integer = 111
            Dim num2 As Integer = 124
            Dim del As operationdelegate
            del = New operationdelegate(AddressOf obj.add)
            del.BeginInvoke(num1, num2, AddressOf Oncallback, Nothing)
            del = New operationdelegate(AddressOf obj.subtract)
            MessageBox.Show(del.Invoke(num1, num2).ToString)
    End Sub
    
        Private Sub Oncallback(ByVal asynresult As IAsyncResult)
            Dim result As Integer = 0
            Dim obj As AsyncResult = CType(asynresult, AsyncResult)
            Dim c As operationdelegate = CType(obj.AsyncDelegate, operationdelegate)
            result = c.EndInvoke(asynresult)
            MessageBox.Show(result.ToString)
    End Sub
    
    End Class
    
    Public Class demo1
    
        Public Function add(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
            Return num1 + num2
    End Function
    
        Public Function subtract(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
            Return num1 - num2
    End Function
    
    End Class
    

     

    For more information about using multithreading in VB.NET, please refer MSDN document Multithreading in Visual Basic .NET:

    http://msdn.microsoft.com/en-us/library/eed6swsx.aspx

     

    Related threads:

    http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/904de128-4dca-4a18-a48f-048e4761bd44/

    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/58a35975-2a71-4adc-86dd-5fc9d4605717/

    http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/6e84ea77-a77c-4e05-a641-ffd8866d3f94/

    http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/e1d8d23e-753d-48f7-b350-87832e0b95d6/

     

    For more FAQ about Visual Basic .NET General, please see Visual Basic .NET General FAQ

     

     



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, April 10, 2009 8:33 AM

All replies

  • .NET applications can perform multiple tasks simultaneously using multithreading in which individual tasks execute on separate threads. It improves the performance and responsiveness of the applications. Generally, we need to note the following tips when we are multithreading:

     

    ·  Use the BackgroundWorker class. BackgroundWorker is supplied by .NET Framework2.0. It is easy to use for executing an operation on a separate thread. There is a full explanation which includes its functionalities and the code samples in the MSDN document. Read the respective document to understand its usage.

     

    ·  Share data between multiple threads in the application. We need to use the thread synchronization mechanism to avoid corrupting the data.

     

    ·  Modify the property of the form and its controls in the main UI thread from the worker thread. When the code in the worker thread will modify the property of the form and its controls in the main UI thread, we need to use the Control.Invoke(or BeginInvoke) method to marshal the call to the correct main UI thread.

     

    ·  Exception handling. If we don’t handle exceptions in the thread function, the unhandled exception will force the hosting process to exit.

     

    ·  Using asynchronous method invocation. Asynchronous method invocation is a feature supplied by CLR to fork the stream of execution into two branches. CLR implements this mechanism by using a work queue. It will package the method parameters and target method address into one request message and queues this message to a process-wide work queue. CLR maintains one thread pool to process the every request in that queue.

     

    You can use the asynchronous method invocation feature in a .NET application via a delegate object. Take a look at the following code snippet about how to use it.

     

    For more information about asynchronous method invocation, please refer to the following articles:

    1)   .NET Delegates: Making Asynchronous Method Calls in the .NET Environment

    http://msdn.microsoft.com/en-us/magazine/cc301332.aspx

    2)   Asynchronous Method Invocation

    http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx

    Imports System.Runtime.Remoting.Messaging
    
    Public Class Form1
    
        Public Delegate Function operationdelegate(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim obj As New demo1
            Dim num1 As Integer = 111
            Dim num2 As Integer = 124
            Dim del As operationdelegate
            del = New operationdelegate(AddressOf obj.add)
            del.BeginInvoke(num1, num2, AddressOf Oncallback, Nothing)
            del = New operationdelegate(AddressOf obj.subtract)
            MessageBox.Show(del.Invoke(num1, num2).ToString)
    End Sub
    
        Private Sub Oncallback(ByVal asynresult As IAsyncResult)
            Dim result As Integer = 0
            Dim obj As AsyncResult = CType(asynresult, AsyncResult)
            Dim c As operationdelegate = CType(obj.AsyncDelegate, operationdelegate)
            result = c.EndInvoke(asynresult)
            MessageBox.Show(result.ToString)
    End Sub
    
    End Class
    
    Public Class demo1
    
        Public Function add(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
            Return num1 + num2
    End Function
    
        Public Function subtract(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
            Return num1 - num2
    End Function
    
    End Class
    

     

    For more information about using multithreading in VB.NET, please refer MSDN document Multithreading in Visual Basic .NET:

    http://msdn.microsoft.com/en-us/library/eed6swsx.aspx

     

    Related threads:

    http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/904de128-4dca-4a18-a48f-048e4761bd44/

    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/58a35975-2a71-4adc-86dd-5fc9d4605717/

    http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/6e84ea77-a77c-4e05-a641-ffd8866d3f94/

    http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/e1d8d23e-753d-48f7-b350-87832e0b95d6/

     

    For more FAQ about Visual Basic .NET General, please see Visual Basic .NET General FAQ

     

     



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, April 10, 2009 8:33 AM
  • you forgot to mark this one as the answer to your own question
    Doug [Remember to mark replies as helpful (if they are) or as the answer]
    • Proposed as answer by _-Steve-_ Friday, April 10, 2009 8:46 AM
    • Unproposed as answer by Martin Xie - MSFT Friday, April 10, 2009 10:30 AM
    Friday, April 10, 2009 8:35 AM