user1113039
user1113039

Reputation: 45

How can we run two threads paralleling?

public class Program
{
    static void Main(string[] args)
    {
        var myThread = new TestThread();
        Thread t = new Thread(new ThreadStart(myThread.PrintName));
        Thread t1 = new Thread(new ThreadStart(myThread.PrintType));
        t.Start();
        t1.Start();
        Console.Read();
    }
}

public class TestThread
{
    public void PrintName()
    {
        for (int i = 1; i <= 50; i++)
        {
            Console.WriteLine("Name {0}", i);
        }
    }

    public void PrintType()
    {
        for (int i = 100; i <= 180; i++)
        {
            Console.WriteLine("Type {0}", i);
        }
    }
}

Here How can i fixed it show that i can generate output in sequential i.e first output of Name then only output of Type... Also I want to know about using Lock() in threads? where can i get good example. I am beginners in threading and need v.simple example.

Upvotes: 2

Views: 191

Answers (5)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112712

The purpose of threads is to allow things to happen at the same time. If you want things to happen one after another (i.e. sequentially), then do not use threads:

var obj = new TestThread();      
obj.PrintName();
obj.PrintType();
Console.Read(); 

Or put PrintName and PrintType into the same thread, in order to keep the UI responsive:

var myThread = new TestThread();
Thread t = new Thread(new ThreadStart(
    () => { 
        myThread.PrintName(); myThread.PrintType();
    } 
));
t.Start();
// Do things in the UI meanwhile
for (i = 0; i < 100; i++) {
    Console.WriteLine("UI thread {0}", i);
}
Console.Read();

Upvotes: 1

Sascha
Sascha

Reputation: 10347

You can look at this

There are multiple other articles, just google for 'introduction threading c#'.

Upvotes: 1

Louis Kottmann
Louis Kottmann

Reputation: 16648

That will do the trick, you should read carefully and try to do it with 3 loops by yourself:

private static void SimpleLockTest()
        {
            Task[] myTasks = new Task[2];
            myTasks[0] = Task.Factory.StartNew(() =>
            {
                LockTestThreadOne();
            });
            myTasks[1] = Task.Factory.StartNew(() =>
            {
                LockTestThreadTwo();
            });
            Task.WaitAll(myTasks);
            Console.WriteLine("Done, press ENTER to quit");
            Console.ReadLine();
        }

        private static object locker = new object();
        private static void LockTestThreadOne()
        {
            Monitor.Enter(locker);
            for (int i = 1; i <= 50; i++)
            {
                Console.WriteLine("Name {0}", i);
                Monitor.Pulse(locker);
                Monitor.Wait(locker);                    
            }
            Monitor.Exit(locker);            
        }

        private static void LockTestThreadTwo()
        {
            Monitor.Enter(locker);
            for (int i = 100; i <= 180; i++)
            {
                Console.WriteLine("Type {0}", i);
                Monitor.Pulse(locker);
                Monitor.Wait(locker, 10);                    
            }
            Monitor.Exit(locker);
        }

Upvotes: 0

Reza ArabQaeni
Reza ArabQaeni

Reputation: 4908

Try this:

var myThread = new TestThread();
var x=Task.Factory.StartNew(() => myThread.PrintName());
x.ContinueWith(p => PrintType());
x.Wait();

Upvotes: 1

dotnetstep
dotnetstep

Reputation: 17485

//This will run two operation in sequence.   
public class TestThread
{
    public object obj = new object();
    public void PrintName()
    {
        Monitor.Enter(obj);
        for (int i = 1; i <= 50; i++)
        {
            Console.WriteLine("Name {0}", i);
        }
        Monitor.Exit(obj);
    }

    public void PrintType()
    {
        Monitor.Enter(obj);
        for (int i = 100; i <= 180; i++)
        {
            Console.WriteLine("Type {0}", i);
        }
        Monitor.Exit(obj);
    }
}

Upvotes: 0

Related Questions