Вопрос:

Make user choose how many threads he want?

c# multithreading

75 просмотра

3 ответа

69 Репутация автора

This question already has an answer here:

I'm trying to make a C# tool where the user can choose how many threads he want for the tool to execute at the same time. So for example he put "10" in the threadsTextBox so when he will click the startButton the program will start and will execute the needed task on 10 threads at the same time.

Here is the current code I have for 3 threads, but I don't know how to make the user able to choose how many threads he want the program to use.

static void Main(string[] args)
{
    Task task1 = Task.Factory.StartNew(() => doStuff());
    Task task2 = Task.Factory.StartNew(() => doStuff());
    Task task3 = Task.Factory.StartNew(() => doStuff());

    Task.WaitAll(task1, task2, task3);
            Console.WriteLine("Done !");
}

static void doStuff()
{
    // do stuff here
}

Thanks in advance.

Автор: Stéphanie Autire Источник Размещён: 08.11.2017 11:51

Ответы (3)


2 плюса

16764 Репутация автора

This seems like an ideal use of arrays:

static void Main(string[] args) {
    var tasks = new Task[n];
    for (int j1 = 0; j1 < n; ++j1)
        tasks[j1] = Task.Factory.StartNew(() => doStuff());

    Task.WaitAll(tasks);
            Console.WriteLine("Done !");
}
Автор: NetMage Размещён: 08.11.2017 11:54

-1 плюса

1 Репутация автора

Maybe you should use an Array with the Length the user wants and you just initalize every Element of the Array with your Task.Factory.StartNew(()); ? Hope this helps a bit ^^

Автор: Starscream Размещён: 08.11.2017 11:55

0 плюса

34893 Репутация автора

The simplest approach would be to allow the user to enter the number of threads as a command-line argument.

int numberOfThreads = int.Parse(args[0]);

In this example I use Enum.Range to generate an IEnumerable with the right number of elements, then use LINQ to start the tasks.

static void Main(string[] args)
{
    int numberOfThreads = int.Parse(args[0]);
    Task.WaitAll(
        Enum.Range(0, numberOfThreads)
            .Select(
                Task.Factory.StartNew(() => doStuff())
            )
            .ToArray()
        )
    );
    Console.WriteLine("Done !");
}

Or you could just use Parallel.For, depending if you really need to have those Tasks.

static void Main(string[] args)
{
    int numberOfThreads = int.Parse(args[0]);

    Parallel.For(1, numberOfThreads, (i) => doStuff());
    Console.WriteLine("Done !");
}

If you need more iterations but want to limit the number of threads, you can use ParallelOptions. In this example I allow for 100 iterations, but only using the number of threads specified by the user.

static void Main(string[] args)
{
    int numberOfThreads = int.Parse(args[0]);

    Parallel.For(
        1, 
        100, 
        new ParallelOptions
        {
            MaxDegreeOfParallelism = numberOfThreads
        },
        (i) => doStuff()
    );
    Console.WriteLine("Done !");
}
Автор: John Wu Размещён: 09.11.2017 12:06
Вопросы из категории :
32x32