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
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
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 ?
How does the data get passed to the second function ?
Have you seen section "Retrieving data with callback methods" in 2nd link?
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.
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....
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.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
or alternativley...
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
dim b as new backgroundworker
addhandler b.dowork, addressof ping_function
b.runworkerasync(Ip_address)
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
https://support.microsoft.com/en-us/kb/315577
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