CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2009
    Posts
    3

    Question Console threading

    Greetings fellow programmers!

    I've got a slight problem i want you to help me with

    I want to create a console application that can be started from the command prompt, with a
    argument specifying how many threads to be created.
    Each thread must print out their number, which
    it gets when it is started, 10 times in a row, followed by new line.
    This print is repeated as long as
    a common stop-variable is set to "false". The main program that starts all the threads must
    stop all the threads via the common stop variable when you press enter.

    This is todays assignment for yall!

    TIA

    /Slobo

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: Console threading

    Finished! Do I get a prize now?

    Anyway, this forum isn't a "do my homework for me" kind of forum. It's more of a "help people who get stuck" kind of forum. In other words, we can't help you unless you get stuck on something and show us where you got stuck.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Console threading

    Seems easy enough. Pay more attention in class

    Seriously dude, show us your code, then we show you our code...

  4. #4
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Console threading

    This is easy:
    check your argument and create an array of threads. That's it!

  5. #5
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Console threading

    Try this :

    Code:
    class Program
    {
        static void Main(string[] args)
        {
            RunThreads(10);
        }
    
        static private void RunThreads(int threadCount)
        {
            Thread[] threads = new Thread[threadCount];
    
            for (int threadIndex = 0; threadIndex < threadCount; ++threadIndex)
            {
                threads[threadIndex] = new Thread(new ParameterizedThreadStart(ThreadFunction));
                threads[threadIndex].Start(threadIndex);
            }
    
            foreach (Thread thread in threads)
            {
                thread.Join();
            }
        }
    
        static private void ThreadFunction(object state)
        {
            Console.WriteLine("I will do my own homework in future {0}", state);
        }
    }


    Darwen.
    Last edited by darwen; November 26th, 2009 at 08:47 AM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  6. #6
    Join Date
    Nov 2009
    Posts
    3

    Re: Console threading

    First of all, thanks alot for the help!
    I'm a 100% newbie to this so nothing makes any sense, not even after 10 hours of googling and studying through course material. The assignment isn't adapted for our previous knowledge, especially not in console programming. It's my first assignment of this kind ever.
    I'm not trying to shoulder the blame at my laziness, it's just not making any sense to me (yet).
    Mutant_Fruit: I completely understand your reaction!

    Thanks again dudes(and girls)!

  7. #7
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Console threading

    I hope he doesn't hand in my code snippet without reading and trying it out first... ha ha ha !

    Darwen
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  8. #8
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Console threading

    Quote Originally Posted by Slobodan46 View Post
    I'm a 100% newbie to this so nothing makes any sense....

    Newbie and then trying to handle multiple threads?
    Maybe you should start with some low level exercises?

  9. #9
    Join Date
    Nov 2009
    Posts
    3

    Re: Console threading

    I've checked your code again and the problem with it is that I want to choose the number of threads by myself by entering it in the console. I've put in some code as a comment and wonder how to implement it in the code. I guess that I won't write RunThreads(10); but I guess that it is connected to the static private void RunThreads(int threadCount). Do you understand my problem? Thanks for bearing with me.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    
    namespace lab2
    {
        class Program
        {
            static void Main(string[] args)
               
            {
                //string instr = Console.ReadLine();
                //Int64 inint = Int64.Parse(instr);
                RunThreads(10);
            }
    
            static private void RunThreads(int threadCount)
            {
                Thread[] threads = new Thread[threadCount];
    
                for (int threadIndex = 0; threadIndex < threadCount; ++threadIndex)
                {
                    threads[threadIndex] = new Thread(new ParameterizedThreadStart(ThreadFunction));
                    threads[threadIndex].Start(threadIndex);
                }
    
                foreach (Thread thread in threads)
                {
                    thread.Join();
                }
            }
    
            static private void ThreadFunction(object state)
            {
                Console.WriteLine("Stupid says what? {0}", state);
            }
        }
    }

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Console threading

    Quote Originally Posted by Slobodan46 View Post
    I've checked your code again and the problem with it is that I want to choose the number of threads by myself by entering it in the console. ... Do you understand my problem?
    We understand, but understand that we aren't going to do your homework for you.

    What you need to do is to break the problem down and determine what is missing from your program.

    Consider the following code snippet:
    Code:
     
    static void Main(string[] args)
    {
      RunThreads(10);
    }
    Since your requirement is to specify the number of threads in the command line, you need to figure out how to change the hardcoded 10 value in the RunThreads(10); statement into some value that gets passed in:
    Code:
     
    static void Main(string[] args)
    {
      RunThreads( threadCount );  // TODO: threadCount needs to be passed in
    }
    If the RunThreads method has been coded properly so that it creates a number of threads based on the value passed into the method, your work is done.

    The secret to solving this is that string[] args contains an array of command line arguments.

    Given that, take a look at this article and see if you can figure out what you need to do (hint: without error checking, it's one line of code).

    http://www.c-sharpcorner.com/UploadF...dLineArgs.aspx

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured