-
November 25th, 2009, 09:07 AM
#1
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
-
November 25th, 2009, 09:43 AM
#2
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.
-
November 25th, 2009, 10:20 AM
#3
Re: Console threading
Seems easy enough. Pay more attention in class
Seriously dude, show us your code, then we show you our code...
-
November 26th, 2009, 05:15 AM
#4
Re: Console threading
This is easy:
check your argument and create an array of threads. That's it!
-
November 26th, 2009, 08:40 AM
#5
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.
-
November 26th, 2009, 11:10 AM
#6
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)!
-
November 26th, 2009, 02:28 PM
#7
Re: Console threading
I hope he doesn't hand in my code snippet without reading and trying it out first... ha ha ha !
Darwen
-
November 27th, 2009, 12:24 AM
#8
Re: Console threading
Originally Posted by Slobodan46
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?
-
December 1st, 2009, 09:00 AM
#9
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);
}
}
}
-
December 2nd, 2009, 12:56 PM
#10
Re: Console threading
Originally Posted by Slobodan46
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|