CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Hybrid View

  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?

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