Results 1 to 4 of 4

Thread: Thread

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    122

    Thread

    Is there anyway to return a value from a thread?

    thanks,

    -zd

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: Thread

    I always point people to this, and its an excellent resource to understand why you want to use a thread to begin with, and highlights various scenarios and solutions:

    http://www.albahari.com/threading/threading.pdf

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    122

    Re: Thread

    Nemaroller,

    this is probably one of the best examples on threading i have ever seen.

    thanks,

    -zd

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Thread

    In .NET 2.0 the solution is simple in many cases. If you use a BackgroundWorker to implement your background thread then getting data into and out of the thread is dead easy.

    To start the ball rolling you call the BGW's RunWorkerAsync method and optionally pass a single object as a parameter. This will raise the DoWork event on a background thread, thus your DoWork event handler is executed on that background thread. If you passed in a parameter then you get this back from the e.Argument property. The object can be anything you like, so it can contain as much data as you like.

    At the end of the DoWork event handler you can optionally assign an object to the e.Result property. Once the method completes the RunWorkerCompleted event is raised on the UI thread, thus your RunWorkerCompleted event handler is executed on the UI thread. If you set e.Result in the DoWork event handler then you get that data back from the e.Result property in the RunWorkerCompleted event handler. Just like the data you passed in, this single object can be anything you like, so it can contain as much data as you like.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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