1

I have a method that receives a parameter and i want to call asynchronously when a click method is called.

    Private Sub audioBox(ByVal message As String)
        cls_utilidades.Audio_Box(" String ")
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)_
    Handles Button1.Click
        'call the method
    End Sub
Marztres
  • 460
  • 8
  • 15

2 Answers2

2

There are at least two ways to do it using TPL (Task Parallel Library, .NET 4.0+):

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
                                                           Handles Button1.Click
  System.Threading.Tasks.Task.Factory.StartNew(AddressOf audioBox, "message")
  'or
  System.Threading.Tasks.Task.Factory.StartNew(Sub() audioBox("message"))
End Sub

This can get more complicated than it seems, because you should also handle exceptions, cancellation etc., same with any multi-threaded approach.

If you are up for that, look into TPL, good framework - lots to learn there.

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • 1
    OP tagged with VS2008 (which supports up to .NET 3.5), so this is not applicable. There are the reactive extensions, but they do not support the whole TPL http://msdn.microsoft.com/en-us/data/gg577609 – J... Jan 17 '14 at 17:14
  • @J...: Oops, did not notice that. That's why you did not mention it. :) I will leave it here anyway, maybe OP will consider upgrading. – Victor Zakharov Jan 17 '14 at 17:16
  • [Can I use the task parallel library in a .Net 3.5 project?](http://stackoverflow.com/questions/2987439/can-i-use-the-task-parallel-library-in-a-net-3-5-project) – Victor Zakharov Jan 17 '14 at 17:17
  • Async and TPL are big reasons, to be sure! – J... Jan 17 '14 at 17:17
  • @J...: Yeah, `async` is another big deal, but I keep thinking in terms of .NET 4.0, just cause many customers are still using XP and Windows 2003, where 4.5 simply does not work. – Victor Zakharov Jan 17 '14 at 17:18
1

There are a lot of ways to do this. You could use a BackgroundWorker, start a plain Thread, use the ThreadPool, etc. Here is an example using the threadpool.

You have to rewrite your method to match the WaitCallback signature (which takes a plain Object):

'be sure to add
Imports System.Threading.ThreadPool

'...    

'need an object type to pass
Private Class AudioBoxArgs
    Public Message As String
End Class

Private Sub audioBox(ByVal state As Object)
   cls_utilidades.Audio_Box(DirectCast(state, AudioBoxArgs).Message)
End Sub

then in your click handler :

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
                                                            Handles Button1.Click
   Dim someMessage As New AudioBoxArgs()
   someMessage.Message = "someMessage"
   QueueUserWorkItem(AddressOf audioBox, someMessage)
End Sub

As with all asynchronous code, you should make sure that cls_utilidades.Audio_Box() is a safe method to call from a separate thread. If you're not yet familiar with thread safety it's something you will have to start learning.

J...
  • 30,968
  • 6
  • 66
  • 143