Calling a method on a sperate thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kplkumar@gmail.com

    #1

    Calling a method on a sperate thread

    I want to call a method passing a parameter. I want to do this call on
    a seperate thread. For example,

    public class Foo
    {
    public static void FooSend(Message message)
    {
    // Start a seperate thread to call Send(message) method
    // everytime someone calls this FooSend method
    }

    private static void Send(Message message)
    {
    // send the message
    }
    }

    I know the threadstart delegate takes only methods that has no
    arguments. But in my case I want to pass a parameter to the method
    call.

    Can someone help me here? Thanks.

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Calling a method on a sperate thread

    Actually, in .NET 2.0, there is a ParameterizedTh readStart method which
    takes an object parameter that you can pass to it. It's not really needed,
    since you can use anonymous delegates in C# 2.0 to get around that.

    However, I'll start with .NET 1.1 and before first. In that case, you
    want to create a class:

    public class FooSendInvoker
    {
    // The parameter to pass to send.
    private Message message;

    public FooSendInvoker( Message message)
    {
    this.message = message;
    }

    // Called by the thread.
    public void Invoke()
    {
    // Make the call.
    Foo.FooSend(mes sage);
    }
    }

    Then, in your code, you would do this:

    // Create the invoker, passing the message.
    FooSendInvoker invoker = new FooSendInvoker( message);

    // Create the thread.
    Thread t = new Thread(new ThreadStart(inv oker.Invoke));

    // Run.
    t.Start();

    In .NET 2.0, you can declare a method like this:

    public static void FooSendInvoker( object message)
    {
    // Cast to a message.
    Message m = (Message) message;

    // Make the call.
    Foo.FooSend(m);
    }

    And then in your code, you can use the ParameterizedTh readStart
    delegate, like so:

    Thread t = new Thread(FooSendI nvoker);

    // Call, passing the message.
    t.Start(message );

    But, C# 2.0 also makes it much easier to call the delegate, since you
    can do this:

    Thread t = new Thread(
    delegate()
    {
    // Just make the call.
    FooSend(message );
    });

    // Run the thread.
    t.Start();

    The message variable will be passed along to the method on the thread
    and executed.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m


    <kplkumar@gmail .com> wrote in message
    news:1136996703 .108669.25950@o 13g2000cwo.goog legroups.com...[color=blue]
    >I want to call a method passing a parameter. I want to do this call on
    > a seperate thread. For example,
    >
    > public class Foo
    > {
    > public static void FooSend(Message message)
    > {
    > // Start a seperate thread to call Send(message) method
    > // everytime someone calls this FooSend method
    > }
    >
    > private static void Send(Message message)
    > {
    > // send the message
    > }
    > }
    >
    > I know the threadstart delegate takes only methods that has no
    > arguments. But in my case I want to pass a parameter to the method
    > call.
    >
    > Can someone help me here? Thanks.
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Calling a method on a sperate thread

      kplku...@gmail. com wrote:[color=blue]
      > I want to call a method passing a parameter. I want to do this call on
      > a seperate thread.[/color]

      Have a look at
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


      Jon

      Comment

      • kplkumar@gmail.com

        #4
        Re: Calling a method on a sperate thread

        Thanks Nicolas. That helped. I am kinda new to C# and .NET, need to
        implement something real fast. Thanks again for the help.

        Comment

        Working...