|
-
Jan 3rd, 2010, 10:58 AM
#1
Thread Starter
Hyperactive Member
Returning result in multithreading
How do I call a function with System.Threading with parameters and get the return?
Normally my code would be:
Code:
StrInput = GetString("A")
How can I do this with multi-threading??
Also. Is it possible to call this function several times without having to create a new thread each time or a sub that I then run as a seperate thread?
Thanks!
Last edited by samtheman; Jan 11th, 2010 at 03:22 PM.
If you found any of my posts helpful then please rate them.
CodeBank
Form Fading Effects in VB.NET and C#
-
Jan 4th, 2010, 10:38 AM
#2
Re: Returning result in multithreading
If you set up a function on a separate thread, you would call Thread.Start. Naturally, there can be no return value, directly, or there is no point in multithreading. When you call a function normally, the calling function waits for a return. Doing that with threading defeats the whole point of multithreading, as the current thread would have to wait for the background thread to finish.
Instead, you would have to either raise an event from the background thread (the BackGroundWorker object will do this automatically, but you can do it yourself easily enough), or do something else to signal the calling thread that the background thread is finished. The event is certainly the most common solution, so I won't go into the others, as they would tend to be specialized.
On the other hand, based on the next question, I either totally misunderstand you, or you are missing something as to what multithreading is, or both. Could you clarify that request?
My usual boring signature: Nothing
 
-
Jan 11th, 2010, 03:21 PM
#3
Thread Starter
Hyperactive Member
Re: Returning result in multithreading
yo
I'm running the Download and CopyFile features from My.Computer. I want to run it as a seperate thread because I don't want the GUI to freeze up while I'm doing some processing.
If I use backgroundworker -- how do I return a completed variable? You're right when you say the idea is to not return, or there'd be no point in multithreading, but I want something like:
Code:
While DoneTask = False
System.Threading.Thread.Sleep(500)
I know this will still freeze the UI a bit. How would I wait for the second thread to complete?
many thankyous
If you found any of my posts helpful then please rate them.
CodeBank
Form Fading Effects in VB.NET and C#
-
Jan 11th, 2010, 03:25 PM
#4
Re: Returning result in multithreading
You want to wait for the second thread to complete? Well, you say you're using BackGroundWorkers....so you can setup a Do While loop:
vb.net Code:
Do While BackgroundWorker1.IsBusy() = True 'Do some work here Loop
Since you're downloading files, there is an Asynchronous Method to download files & report the progress, as well as Event Handlers that can fire after the file is complete.
-
Jan 11th, 2010, 03:45 PM
#5
Re: Returning result in multithreading
If you are using the BackgroundWorker you can store a value in the e.Result property in the DoWork event and then access it in the RunWorkerCompleted event (which will be raised as soon as the worker has finished).
Last edited by chris128; Jan 11th, 2010 at 06:41 PM.
-
Jan 11th, 2010, 06:34 PM
#6
Thread Starter
Hyperactive Member
Re: Returning result in multithreading
can't return the argument because I can't return a string to System.ComponentModel
If you found any of my posts helpful then please rate them.
CodeBank
Form Fading Effects in VB.NET and C#
-
Jan 11th, 2010, 06:42 PM
#7
Re: Returning result in multithreading
Sorry I didnt mean the e.Argument property, I meant the e.Result property. So from your DoWork event handler, something like this:
vb Code:
Dim str As String = "hello"
e.Result = str
And then in the RunWorkerCompleted event handler:
vb Code:
Dim ResultString As String
ResultString = CStr(e.Result)
MessageBox.Show(ResultString)
-
Jan 11th, 2010, 06:49 PM
#8
Thread Starter
Hyperactive Member
Re: Returning result in multithreading
Nice one mate,
If I do threading.thread.sleep while IsBusy = True then the form will still freeze won't it -- even if I'm only doing it for a few milliseconds at a time?
I've got a LOT of code in .net that I want to run behind asyncronously with form but syncronously with the other code. By this I mean until one process completes I don't want the other stuff firing. Would I be better off just dividing it all into a several bg workers and just kicking the next one off at each EventCompleted?
cheers
If you found any of my posts helpful then please rate them.
CodeBank
Form Fading Effects in VB.NET and C#
-
Jan 11th, 2010, 06:50 PM
#9
Thread Starter
Hyperactive Member
Re: Returning result in multithreading
Also can I call the same bg workers a few times? My plan is to have it like a sub where it varies depending on what it does.
If you found any of my posts helpful then please rate them.
CodeBank
Form Fading Effects in VB.NET and C#
-
Jan 11th, 2010, 06:57 PM
#10
Re: Returning result in multithreading
If I do threading.thread.sleep while IsBusy = True then the form will still freeze won't it -- even if I'm only doing it for a few milliseconds at a time?
Well, if you just fire off the next BG Worker in the completed events, then will you need to ever do that? You can call the same BG Worker multiple times, but it cannot be busy, else you'll get an error.
-
Jan 11th, 2010, 07:07 PM
#11
Thread Starter
Hyperactive Member
Re: Returning result in multithreading
Exactly what I thought. If I just stack them up in eventcompleted i'll be ok. But, if it can't be busy then I must check IsBusy = False before I run the command. Problem is I will have to use sleep to wait between commands finishing up, which means form will still freeze...
If I call a function from the bgworker its still running in the new thread?
Cheers
If you found any of my posts helpful then please rate them.
CodeBank
Form Fading Effects in VB.NET and C#
-
Jan 11th, 2010, 07:12 PM
#12
Re: Returning result in multithreading
 Originally Posted by samtheman
Exactly what I thought. If I just stack them up in eventcompleted i'll be ok. But, if it can't be busy then I must check IsBusy = False before I run the command. Problem is I will have to use sleep to wait between commands finishing up, which means form will still freeze...
If I call a function from the bgworker its still running in the new thread?
Cheers
You can do a little cheap bit of code:
vb.net Code:
Do
If <BackgroundWorker Name>.IsBusy() = False Then <BackgroundWorkerName>.RunWorkerAsync() : Exit Do
Loop
I don't really recommend that...and I'm probably going to get flamed for suggesting that because that could get your program stuck in an infinite loop & freeze up anyway.
Why would your workers be busy if they haven't been called already?
For your last question, if you want to call a function that affects or relies on data from the main thread (aka the GUI/Interface Thread), you will have to use delegates.
-
Jan 12th, 2010, 02:38 AM
#13
Thread Starter
Hyperactive Member
Re: Returning result in multithreading
Hey it's because I'm passing via argument to the background process several times based on the results from previous threads.
If you found any of my posts helpful then please rate them.
CodeBank
Form Fading Effects in VB.NET and C#
-
Jan 12th, 2010, 05:30 AM
#14
Re: Returning result in multithreading
f I call a function from the bgworker its still running in the new thread?
Yes, anything you call from the DoWork event handler will still be running in the background thread
-
Jan 12th, 2010, 12:33 PM
#15
Thread Starter
Hyperactive Member
Re: Returning result in multithreading
So there's no way of waiting without sleeping the form and causing freezes, unless starting the background worker is the VERY last thing I do from the GUI thread
If you found any of my posts helpful then please rate them.
CodeBank
Form Fading Effects in VB.NET and C#
-
Jan 12th, 2010, 01:43 PM
#16
Re: Returning result in multithreading
Huh? I'm confused, what do you want your form to wait for? If you want it to wait for the completion of the background thread, we have just discussed the RunWorkerCompleted event - you can put your code in there that you want to be executed after the background thread has done its work, then you dont need to sleep the UI thread.
-
Jan 13th, 2010, 08:22 AM
#17
Thread Starter
Hyperactive Member
Re: Returning result in multithreading
Yeah but I have code running after the background worker. Don't worry - it's not resolved but I think I know what i've got to do.
Code:
'Call background worker
'Then do something
If you found any of my posts helpful then please rate them.
CodeBank
Form Fading Effects in VB.NET and C#
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|