Results 1 to 3 of 3

Thread: [1.0/1.1] Best way to return the exit code of a process run in a thread? [C# 2002]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

    Question [1.0/1.1] Best way to return the exit code of a process run in a thread? [C# 2002]

    Hopefully quick & simple question - I start a thread that launches a process and gets the ExitCode of that process and I need to somehow return that information (the integer exit code of the process) back to the UI which spawned the thread (and this can happen when the thread ends/terminates, no need to send it immediatly seeing as each thread is only responsible for launching a process they will end gracefully after getting the exit code anyways).

    So - is there a nice way to return the exit code of a process launched from a thread to the UI (that spawned the thread)? Some way to return a value like we would do with a function for example?

    I got it to work one way by creating a ReturnCode() delegate that I can launch as the last step in my Thread, this will pass the processes exit code from the thread to the UI but I'm not sure this is the best solution as there is no need to fire the delegate while the thread is running - maybe simply have the thread "return" the processes exit code?

    Anyways - I was just wondering if there was a better way to accomplish this other then a delegate (which works fine) - any help would be much appreciated.
    Thanks,

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

    Re: [1.0/1.1] Best way to return the exit code of a process run in a thread? [C# 2002]

    Threads don't actually return anything because they themselves are not methods. Normally you don't even keep a reference to the Thread object once you've called its Start method.

    The way to get data back to the UI thread is by delegation, which is what you're doing. Just note that delegation is only required if you actually want to access a control or controls.
    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

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [1.0/1.1] Best way to return the exit code of a process run in a thread? [C# 2002]

    If you don't keep a reference to a running thread then you open yourself up to a whole world of pain. (eg. if it hangs or races you have a problem on your hands if you need to kill it)

    One dead easy way to return a status code is to set the Name of the thread to something meaningful once it has got to a point when the return code is known (you can only set it once if i remember correctly). Then you can use a callback function to report that the thread has finished and what its return code was. There are several other (better and safer) ways to achieve the same result but the name property is basically never used for anything else anyway, so you can treat it like a "Tag" property.
    I don't live here any more.

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