Threading test problem. 
Author Message
 Threading test problem.

Hi,

Following up on my Thread array post the following Thread test gives
unexpected output - none of my threads are alive. Very similar code except
not using arrays and creating thread1 to thread5 and myTest1 to myTest5
manually worked with the expected output. I don't get why the code is not
working just because I've turned the threads into an array.

Here's the ouput I get from this code (below):
----------------------------------------------
Start Begin
Thread 0 IsAlive status: False
Thread 1 IsAlive status: False
Thread 2 IsAlive status: False
Thread 3 IsAlive status: False
Thread 4 IsAlive status: False
Start End
----------------------------------------------

Here's something like the ouput I expected:
----------------------------------------------
Start Begin
Pos 1 In Thread one
Pos 1 In Thread two
Pos 1 In Thread three
Pos 1 In Thread four
Pos 1 In Thread five
Thread 0 IsAlive status: True
Thread 1 IsAlive status: True
Thread 2 IsAlive status: True
Thread 3 IsAlive status: True
Thread 4 IsAlive status: True
Start End
Pos 2 In Thread one
Pos 2 In Thread two
Pos 2 In Thread three
Pos 2 In Thread four
Pos 2 In Thread five
----------------------------------------------

public class TestThread
{

  public TestThread()
  {
  }

  public bool Start()
  {
    Console.WriteLine("Start Begin");

    string [] nums = new string[] {"one", "two", "three", "four" };

    MyTest [] myTests = new MyTest[5];
    Thread [] myThreads = new Thread[5];

    for (int i = 0; i < 5; i++)
    {
      myTests[i] = new MyTest(nums[i], i * 1000);
      // Commented out cos values are sucessfully set as expected
      // Console.WriteLine("myTests " + i + ": " + myTests[i].text);
      myThreads[i] = new Thread(new ThreadStart(myTests[i].HandleTest));
    }

    for (int i = 0; i < 5; i++)
    {
      Console.WriteLine("Thread " + i + " IsAlive status: " +
                        myThreads[i].IsAlive);
    }

    Console.WriteLine("Start End");
    return true;

Quote:
}

  public static void Main()
  {
    TestThread tt = new TestThread();
    tt.Start();
  }

Quote:
}

class MyTest
{
  public string text;
  public int sleep;
  public string val;

  public MyTest()
  {
  }

  public MyTest(string Text, int Sleep)
  {
    text = Text;
    sleep = Sleep;
    val = text + " nothing done yet";
  }

  public void HandleTest()
  {
    val = text + " not done.";
    Console.WriteLine("Pos 1 In Thread " + text);
    Thread.Sleep(sleep);
    Console.WriteLine("Pos 2 In Thread " + text);
    val = text + " done.";
  }

Quote:
}



Mon, 13 Dec 2004 21:19:08 GMT  
 Threading test problem.
Matt,

    You need to call the Start method to make your threads run.

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> Hi,

> Following up on my Thread array post the following Thread test gives
> unexpected output - none of my threads are alive. Very similar code except
> not using arrays and creating thread1 to thread5 and myTest1 to myTest5
> manually worked with the expected output. I don't get why the code is not
> working just because I've turned the threads into an array.

> Here's the ouput I get from this code (below):
> ----------------------------------------------
> Start Begin
> Thread 0 IsAlive status: False
> Thread 1 IsAlive status: False
> Thread 2 IsAlive status: False
> Thread 3 IsAlive status: False
> Thread 4 IsAlive status: False
> Start End
> ----------------------------------------------

> Here's something like the ouput I expected:
> ----------------------------------------------
> Start Begin
> Pos 1 In Thread one
> Pos 1 In Thread two
> Pos 1 In Thread three
> Pos 1 In Thread four
> Pos 1 In Thread five
> Thread 0 IsAlive status: True
> Thread 1 IsAlive status: True
> Thread 2 IsAlive status: True
> Thread 3 IsAlive status: True
> Thread 4 IsAlive status: True
> Start End
> Pos 2 In Thread one
> Pos 2 In Thread two
> Pos 2 In Thread three
> Pos 2 In Thread four
> Pos 2 In Thread five
> ----------------------------------------------

> public class TestThread
> {

>   public TestThread()
>   {
>   }

>   public bool Start()
>   {
>     Console.WriteLine("Start Begin");

>     string [] nums = new string[] {"one", "two", "three", "four" };

>     MyTest [] myTests = new MyTest[5];
>     Thread [] myThreads = new Thread[5];

>     for (int i = 0; i < 5; i++)
>     {
>       myTests[i] = new MyTest(nums[i], i * 1000);
>       // Commented out cos values are sucessfully set as expected
>       // Console.WriteLine("myTests " + i + ": " + myTests[i].text);
>       myThreads[i] = new Thread(new ThreadStart(myTests[i].HandleTest));
>     }

>     for (int i = 0; i < 5; i++)
>     {
>       Console.WriteLine("Thread " + i + " IsAlive status: " +
>                         myThreads[i].IsAlive);
>     }

>     Console.WriteLine("Start End");
>     return true;
> }

>   public static void Main()
>   {
>     TestThread tt = new TestThread();
>     tt.Start();
>   }
> }

> class MyTest
> {
>   public string text;
>   public int sleep;
>   public string val;

>   public MyTest()
>   {
>   }

>   public MyTest(string Text, int Sleep)
>   {
>     text = Text;
>     sleep = Sleep;
>     val = text + " nothing done yet";
>   }

>   public void HandleTest()
>   {
>     val = text + " not done.";
>     Console.WriteLine("Pos 1 In Thread " + text);
>     Thread.Sleep(sleep);
>     Console.WriteLine("Pos 2 In Thread " + text);
>     val = text + " done.";
>   }
> }



Mon, 13 Dec 2004 21:29:38 GMT  
 Threading test problem.

Quote:
> >  --I get the idiot of the day award!

> It's early, lets see what happens

Tell me I'm not a contender now!

Thanks and Regards,

..matthew



Mon, 13 Dec 2004 22:15:20 GMT  
 Threading test problem.
Matt,

    My original statement was by no means an attempt at precognition, but I
have to say, you really are making yourself stand out in the crowd of other
contenders =P

    Thank God your day in London is almost over, hmm?

--
               - Nicholas Paldino [.NET MVP]


Quote:
> > >  --I get the idiot of the day award!

> > It's early, lets see what happens

> Tell me I'm not a contender now!

> Thanks and Regards,

> ..matthew



Mon, 13 Dec 2004 22:18:55 GMT  
 Threading test problem.

Quote:

> Matt,

>     My original statement was by no means an attempt at precognition, but I
> have to say, you really are making yourself stand out in the crowd of other
> contenders =P

>     Thank God your day in London is almost over, hmm?

It's been "one of those days" all day.

Thanks v. much for your help.

..matthew



Mon, 13 Dec 2004 22:30:12 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. test test test test

2. test...test...test

3. Problem with thread inside thread

4. DLL issue: Problem with beginning a new thread when one thread gets into an infinite loop

5. Synchronization problem between main thread and worker thread

6. threads, threads, and more threads

7. this is a test message - testing my email program - please ignore it - thank you -bye

8. testing testing

9. How to test (Re: C language test)

10. VC++5: Creating library .\Debug/Test.lib and object .\Debug/Test.exp

11. Testing a console applications input / Testing a files existence

12. Test to debug Outlook Express Problem

 

 
Powered by phpBB® Forum Software