Link to home
Start Free TrialLog in
Avatar of PNRT
PNRT

asked on

VB.Net how to run a function in a seperate thread

Hi Experts
I have two simple functions - one checks the status of a server and returns true or false, the other sends an email based
on data passed to it.    I need to free up the GUI while these functions operate and would like to operate them on separate threads so that the continuous progressbar runs and the users can continue.
Please could someone give me an example of how to run a function like that on a separate thread, how to pass data to it and receive a result from it.  I've seen many examples on the web of the backgroundworker, but none that operate it as a function. Many thanks
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Example code for starting thread here

https://support.microsoft.com/en-us/kb/315577


Private Sub Form1_Load( _
    ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load

    Private trd As Thread
    trd = New Thread(AddressOf ThreadTask)
    trd.IsBackground = True
    trd.Start()
End Sub


Private Sub ThreadTask()
    Dim stp As Integer
    Dim newval As Integer
    Dim rnd As New Random()

    Do
        stp = ProgressBar1.Step * rnd.Next(-1, 2)
        newval = ProgressBar1.Value + stp
        If newval > ProgressBar1.Maximum Then
            newval = ProgressBar1.Maximum
        ElseIf newval < ProgressBar1.Minimum Then
            newval = ProgressBar1.Minimum
        End If

        ProgressBar1.Value = newval

        Thread.Sleep(100)
    Loop
End Sub

Open in new window



Also

https://msdn.microsoft.com/en-us/library/ts553s52(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
Avatar of PNRT
PNRT

ASKER

Thanks, I've seen this many times, but as per my question, I was looking for how to operate a function, sending data to it and receiving a result from it
So do you want to check the server status then send an email based on it?

How does the data get passed to the second function ?
Have you seen section "Retrieving data with callback methods" in 2nd link?
Avatar of PNRT

ASKER

Hi R555B, thanks for the reply, they are just two simple functions the first gets sent the IP of the server to ping and returns true or false.  The second function gets sent, server, port, user/password and email address and sends and email.
Returning true or false if successful.  Both work fine but I was just trying to get the same functionality without freezing the GUI

Hi CodeCruiser.  thanks for the reply.   LOL, if I wasn't confused before, then reading about callback methods really did it.  Sorry.
OK, if I was doing that, I'd do something like this....


dim b as new backgroundworker
addhandler b.dowork, addressof Testsub
dim parameters as string = IP & "//" & server & "//" & port
b.runworkerasync(parameters)


public sub testsub(byval sender as object, byval e as doworkeventargs)
dim sub_params() as string = e.argument.tostring.split("//")
dim IP as string = spilts(0)

ping_function(ip)
mail_function(other parameters...)

end sub

Open in new window



Or something along those lines. Pass all the parameters to a background worker as one big string. Parse them into separate variables and the pass those to the functions.
If youve got multiple servers, ports etc youll want to loop it somehow as well.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or alternativley...

dim b as new backgroundworker
addhandler b.dowork, addressof ping_function
b.runworkerasync(Ip_address)

Open in new window


That may not work as well though as I think the backgroundworker stil expects things to be passed to it as e.Arguments as opposed to a regular parameter.
You may find that way the ping_function returns an exception as its got nothing to ping