8 Replies - 897 Views - Last Post: 11 March 2010 - 08:43 AM Rate Topic: -----

#1 zaurb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-March 10

Experimenting with threading

Posted 11 March 2010 - 06:00 AM

Hello!

I'm learning about multithreading in C# and I would like to create a console application which prints two lines as follows:
Counting down from 10000 to 0: ...
Counting up from 0 to 10000: ...

Instead of "..." I need numbers to change and the rest of the text to remain intact, if possible.

So far I have created the following, but obviously there's something wrong in this code and I can't figure out how to make it work correctly...
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace test_Threading_02
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread t1 = new Thread(CountUp);
            t1.Start();

           Thread t2 = new Thread(CountDown);
           t2.Start();
        }

        static void CountUp()
        {
            Console.Write("Counting up 0 to 1000000:");
            for (int i = 0; i <= 1000000; i++)
            {

                Console.Write("{0}\r".PadRight(27), i++);
            }

        }

        static void CountDown()
        {
            Console.Write("\nCounting down from 1000000 to 0:");
            for (int i = 1000000; i >= 0; i--)
            {
                Console.Write("{0}\r".PadRight(27), i--);
            }
        }
    }
}


This post has been edited by zaurb: 11 March 2010 - 06:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Experimenting with threading

#2 FlashM   User is offline

  • D.I.C Lover
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Experimenting with threading

Posted 11 March 2010 - 06:13 AM

I modified your code a bit. Is this what you had in mind? Could you tell us what is not working as expected?

static void Main(string[] args)
{
    Thread t1 = new Thread(CountUp);
    t1.Start();
}

static void CountUp()
{
    for (int i = 0; i <= 1000000; i++)
    {
        Console.Write("Counting up 0 to 1000000:");
        Console.Write(" {0}\r", i);
        Thread.Sleep(200);
    }

}


Was This Post Helpful? 1
  • +
  • -

#3 zaurb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-March 10

Re: Experimenting with threading

Posted 11 March 2010 - 06:28 AM

View PostFlashM, on 11 March 2010 - 05:13 AM, said:

I modified your code a bit. Is this what you had in mind? Could you tell us what is not working as expected?

static void Main(string[] args)
{
    Thread t1 = new Thread(CountUp);
    t1.Start();
}

static void CountUp()
{
    for (int i = 0; i <= 1000000; i++)
    {
        Console.Write("Counting up 0 to 1000000:");
        Console.Write(" {0}\r", i);
        Thread.Sleep(200);
    }

}



This correction makes the program work better with the single thread, as I understand. But how do I add the second line with count down?

I modified the program as folows, but one line overlays another ...
    class Program
    {
        static void Main(string[] args)
        {
            Thread t1 = new Thread(CountUp);
            t1.Start();

          Thread t2 = new Thread(CountDown);
           t2.Start();


        }

        static void CountUp()
        {
            
            for (int i = 0; i <= 1000000; i++)
            {
                Console.Write("Counting up 0 to 1000000:"); 
                Console.Write(" {0}\r", i);
                Thread.Sleep(200);
            }

        }

        static void CountDown()
        {
            Console.WriteLine();
            for (int i = 1000000; i >= 0; i--)
            {
                Console.Write("Counting down from 1000000 to 0:");
                Console.Write("{0}\r", i);
                Thread.Sleep(200);
            }
        }
    }


This post has been edited by zaurb: 11 March 2010 - 06:31 AM

Was This Post Helpful? 0
  • +
  • -

#4 FlashM   User is offline

  • D.I.C Lover
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Experimenting with threading

Posted 11 March 2010 - 06:41 AM

I know what you would like to accomplish but I have no idea how to do that :-) Hope someone on this forum has more knowledge working with Console... Will follow this topic...
Was This Post Helpful? 0
  • +
  • -

#5 lesPaul456   User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Experimenting with threading

Posted 11 March 2010 - 08:13 AM

The problem is how you are writing the values to the screen. Try something like this instead:

static void Main(string[] args)
{
    Thread t1 = new Thread(CountUp);
    t1.Start();

    Thread t2 = new Thread(CountDown);
    t2.Start();
}

static void CountUp()
{
    for (int i = 0; i <= 1000000; i++)
    {
        Console.CursorLeft = 0; // Set the cursor to the first column in the buffer.
        Console.CursorTop = 0; // Set the cursor to the first row in the buffer.
        Console.Write("Counting up 0 to 1000000: {0}", i);
        Thread.Sleep(200);
    }

}

static void CountDown()
{
    for (int i = 1000000; i >= 0; i--)
    {
        Console.CursorLeft = 0; // Set the cursor to the first column in the buffer.
        Console.CursorTop = 2; // Set the cursor to the third row in the column.
        Console.Write("Counting down from 1000000 to 0: {0}", i);
        Thread.Sleep(200);
    }
}



Hope this helps!
Was This Post Helpful? 2
  • +
  • -

#6 FlashM   User is offline

  • D.I.C Lover
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Experimenting with threading

Posted 11 March 2010 - 08:21 AM

Exactly, this would work... Thanks lesPaul456!!!

static void Main(string[] args)
{
    Thread t1 = new Thread(CountUp);
    t1.Start();

    Thread t2 = new Thread(CountDown);
    t2.Start();
}

static void CountUp()
{
    for (int i = 0; i <= 1000000; i++)
    {
        Console.CursorLeft = 0;
        Console.CursorTop = 0;
        Console.Write("Counting up 0 to 1000000: {0}\r", i);
        Thread.Sleep(500);
    }

}

static void CountDown()
{
    for (int i = 1000000; i >= 0; i--)
    {
        Console.CursorLeft = 0;
        Console.CursorTop = 1;
        Console.Write("Counting down from 1000000 to 0: {0}\r", i);
        Thread.Sleep(500);
    }
}



Actually, the two threads compete with each other, so for a while one thread is updating the Console and then the other one, which is not cool... I believe one thread should be resposible for calculating both counters and writing them to console window...

This post has been edited by FlashM: 11 March 2010 - 08:22 AM

Was This Post Helpful? 0
  • +
  • -

#7 zaurb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-March 10

Re: Experimenting with threading

Posted 11 March 2010 - 08:29 AM

View PostlesPaul456, on 11 March 2010 - 07:13 AM, said:

The problem is how you are writing the values to the screen. Try something like this instead:

static void Main(string[] args)
{
    Thread t1 = new Thread(CountUp);
    t1.Start();

    Thread t2 = new Thread(CountDown);
    t2.Start();
}

static void CountUp()
{
    for (int i = 0; i <= 1000000; i++)
    {
        Console.CursorLeft = 0; // Set the cursor to the first column in the buffer.
        Console.CursorTop = 0; // Set the cursor to the first row in the buffer.
        Console.Write("Counting up 0 to 1000000: {0}", i);
        Thread.Sleep(200);
    }

}

static void CountDown()
{
    for (int i = 1000000; i >= 0; i--)
    {
        Console.CursorLeft = 0; // Set the cursor to the first column in the buffer.
        Console.CursorTop = 2; // Set the cursor to the third row in the column.
        Console.Write("Counting down from 1000000 to 0: {0}", i);
        Thread.Sleep(200);
    }
}



Hope this helps!


This works great! Thank you very much!
Just a small question, why do I get one extra zero in the endo of the second string like in the attached image?
Attached Image
Was This Post Helpful? 0
  • +
  • -

#8 FlashM   User is offline

  • D.I.C Lover
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Experimenting with threading

Posted 11 March 2010 - 08:36 AM

Try to use Counting up 0 to 1000000: {0}\r and see if it helps...
Was This Post Helpful? 0
  • +
  • -

#9 zaurb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-March 10

Re: Experimenting with threading

Posted 11 March 2010 - 08:43 AM

View PostFlashM, on 11 March 2010 - 07:36 AM, said:

Try to use Counting up 0 to 1000000: {0}\r and see if it helps...


tried... didn't work. still a trailing 0 in the end of the second line.
Wierd...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1