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.
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.
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.
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!
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);
}
}
}
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.
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).
Bookmarks