threading parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • grmAbay

    #1

    threading parameter

    Hello,

    This might sound as a stupid question, but how can I pass parameters to a
    thread? Let me rephrase that.

    When I have a method, i declare it like this:

    public void DoSomething(str ing aParameter, int anotherParamete r) {
    ....
    }

    When I want to use this method with a thread I do it like this:
    Thread doSomethingThre ad= new Thread(new ThreadStart(thi s.DoSomething)) ;

    doSomethingThre ad.Start();



    Where do I add the parameters?



  • Jon Skeet

    #2
    Re: threading parameter

    grmAbay <grmAbay@hotmai l.com> wrote:[color=blue]
    > This might sound as a stupid question, but how can I pass parameters to a
    > thread?[/color]

    Not a stupid question at all - see
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • 100

      #3
      Re: threading parameter

      Hi,
      In addition to what you can find in Jon's site I want to give you one more
      idea.
      You can decalre a delegate for the method you want to start in a thread.

      //Delegate and the method itslef might have a return value as well
      delegate void ThreadMethod(st ring aParameter, int anotherParamete r) ;

      public void DoSomething(str ing aParameter, int anotherParamete r) {
      ....
      }

      ThreadMethod starter = new ThreadMethod(Do Something);

      Now you can start DoSomething in a separate thread:

      starter.BeginIn voke(param1, param2, null, null);

      You can use IAsyncResult returned by BeginInvoke and use EndInvoke to wait
      for the thread to finish and get result. You can optain a wait handle,
      checking periodically IAsyncResult.Is Completed or using callback method. You
      have options.

      Begin invoke uses a thread form the thread pool

      More info you can find in MSDN
      ms-help://MS.MSDNQTR.2003 FEB.1033/cpguide/html/cpovrasynchrono usprogrammingo
      verview.htm

      HTH
      B\rgds
      100

      "Jon Skeet" <skeet@pobox.co m> wrote in message
      news:MPG.19b856 a1b9cf16a798a40 6@news.microsof t.com...[color=blue]
      > grmAbay <grmAbay@hotmai l.com> wrote:[color=green]
      > > This might sound as a stupid question, but how can I pass parameters to[/color][/color]
      a[color=blue][color=green]
      > > thread?[/color]
      >
      > Not a stupid question at all - see
      > http://www.pobox.com/~skeet/csharp/threadstart.html
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet/
      > If replying to the group, please do not mail me too[/color]


      Comment

      Working...