I am trying to make a multithreading application. But the input is onely with one thread. But I try to make it with three threads. THis is the program:
class MyThread
{
public int Count;
public Thread Thrd;
public MyThread(string name)
{
Count = 0;
Thrd = new Thread(this.Run);
Thrd.Name = name;
Thrd.Start();
}
// Entry point of thread.
void Run()
{
Console.WriteLine(Thrd.Name + " starting.");
do
{
Thread.Sleep(500);
Console.WriteLine("In " + Thrd.Name +
", Count is " + Count);
Count++;
} while (Count < 10);
Console.WriteLine(Thrd.Name + " terminating.");
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main thread starting.");
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
while (true)
{
Console.Write(".");
Thread.Sleep(100);
if (mt1.Count < 10 && mt2.Count < 10 && mt3.Count < 10)
{
break;
}
Console.WriteLine("Main thread ending.");
}
//do
//{
// Console.Write(".");
// Thread.Sleep(100);
//}
//while (mt1.Count < 10 && mt2.Count < 10 && mt3.Count < 10);
//Console.WriteLine("Main thread ending.");
// Console.ReadKey();
}
}
So it onely displays one thread. And not three threads.
THank you
This has to be the output:
Main thread starting.
.Child #1 starting.
Child #2 starting.
Child #3 starting.
....In Child #1, Count is 0
In Child #2, Count is 0
In Child #3, Count is 0
.....In Child #1, Count is 1
In Child #2, Count is 1
In Child #3, Count is 1
.....In Child #1, Count is 2
In Child #2, Count is 2
In Child #3, Count is 2
.....In Child #1, Count is 3
In Child #2, Count is 3
In Child #3, Count is 3
.....In Child #1, Count is 4
In Child #2, Count is 4
In Child #3, Count is 4
.....In Child #1, Count is 5
In Child #2, Count is 5
In Child #3, Count is 5
.....In Child #1, Count is 6
In Child #2, Count is 6
In Child #3, Count is 6
.....In Child #1, Count is 7
In Child #2, Count is 7
In Child #3, Count is 7
.....In Child #1, Count is 8
In Child #2, Count is 8
In Child #3, Count is 8
.....In Child #1, Count is 9
Child #1 terminating.
In Child #2, Count is 9
Child #2 terminating.
In Child #3, Count is 9
Child #3 terminating.
Main thread ending.
But if I do it like this:
static void Main(string[] args)
{
Console.WriteLine("Main thread starting.");
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
//while (true)
//{
// Console.Write(".");
// Thread.Sleep(100);
// if (mt1.Count < 10 && mt2.Count < 10 && mt3.Count < 10)
// {
// break;
// }
// Console.WriteLine("Main thread ending.");
//}
do
{
Console.Write(".");
Thread.Sleep(100);
}
while (mt1.Count < 10 && mt2.Count < 10 && mt3.Count < 10);
Console.WriteLine("Main thread ending.");
Console.ReadKey();
}
It gives the same result.
If I do this:
Console.WriteLine("Main thread starting.");
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
mt1.Thrd.Join();
mt2.Thrd.Join();
mt3.Thrd.Join();
do
{
Console.Write(".");
Thread.Sleep(100);
} while (mt1.Thrd.IsAlive && mt2.Thrd.IsAlive && mt3.Thrd.IsAlive);
Console.WriteLine("Main thread ending.");
Same result. Just one thread.
I try it like this:
static void Main(string[] args)
{
Console.WriteLine("Main thread starting.");
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
mt1.Thrd.Join();
mt2.Thrd.Join();
mt3.Thrd.Join();
do
{
Console.Write(".");
Thread.Sleep(100);
} while (mt1.Count < 10 || mt2.Count < 10 || mt3.Count < 10);
Console.WriteLine("Main thread ending.");
But still the same result.
oh:
This worked for me!!
Console.WriteLine("Main thread starting.");
// Construct three threads.
MyThread mt1 = new MyThread("Child #1");
MyThread mt2 = new MyThread("Child #2");
MyThread mt3 = new MyThread("Child #3");
mt1.Thrd.Join();
mt2.Thrd.Join();
mt3.Thrd.Join();
do
{
Console.Write(".");
Thread.Sleep(100);
} while (mt1.Thrd.IsAlive || mt2.Thrd.IsAlive || mt3.Thrd.IsAlive);
Console.WriteLine("Main thread ending.");