Results 1 to 17 of 17

Thread: Returning result in multithreading

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    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#

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    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#

  4. #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:
    1. Do While BackgroundWorker1.IsBusy() = True
    2.    'Do some work here
    3. 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.

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    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#

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    1. Dim str As String = "hello"
    2. e.Result = str
    And then in the RunWorkerCompleted event handler:
    vb Code:
    1. Dim ResultString As String
    2. ResultString = CStr(e.Result)
    3.  
    4. MessageBox.Show(ResultString)
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    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#

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    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#

  10. #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.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    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#

  12. #12

    Re: Returning result in multithreading

    Quote Originally Posted by samtheman View Post
    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:
    1. Do
    2.    If <BackgroundWorker Name>.IsBusy() = False Then <BackgroundWorkerName>.RunWorkerAsync() : Exit Do
    3. 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.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    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#

  14. #14
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    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#

  16. #16
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    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
  •  



Click Here to Expand Forum to Full Width