D J
D J

Reputation: 7028

how to execute second method on same instance of thread after finishing first

Suppose I have a thread like

var th = new Thread(new ThreadStart(Method1));
th.Start();
th.Join(); //// wait for Method1 to finish

//// how to execute the Method2 on same thread th?

I want to excute Method2 on same insatnce of thread when it finishes the method Method1.

How can I do that?

Upvotes: 1

Views: 588

Answers (2)

qamar
qamar

Reputation: 1447

If you are using Task Parallel Library you can use continuewith to line up tasks.

Task.Factory.StartNew(() => action("alpha"))
        .ContinueWith(antecendent => action("beta")) 
        .ContinueWith(antecendent => action("gamma"));

Upvotes: 2

Sai Puli
Sai Puli

Reputation: 971

Is this what you're trying to do?

using System;
using System.Threading;

public class Program
{
    public static void Main()
    {
        var th = new Thread(new ThreadStart(() =>
        {
            Method1();
            Method2();
        }

        ));
        th.Start();
        th.Join();
    }

    static void Method1()
    {
        Console.WriteLine("Method 1");
    }

    static void Method2()
    {
        Console.WriteLine("Method 2");
    }
}

Upvotes: 3

Related Questions