-1

I have a multi-line textbox and I want to process each line with multi threads. This is just an example to learn tasks, but I'm going to be doing different tasks with the lines afterwards, not only print them.

For example let's say my textbox contains:

1
2
3
4
5
6
7
8
9
10

Using the following code:

    List<Task> tasks = new List<Task>();

    for (int i = 0; i < txtSearchTerms.Text.Length; i++)
    {
        int tmp = i;
        Task t = new Task(() =>
                        {
                            Console.WriteLine(txtSearchTerms.Text[tmp]);
                        });
        tasks.Add(t);
        t.Start();
    }

    Task.WaitAll(tasks.ToArray());

    foreach(Task t in tasks)
    {
        Console.WriteLine("Task {0}, Status {1}", t.Id, t.Status);
    }

I get the following output:

1




3




4




5




6




7




8




9




1
0
2
Task 1, Status RanToCompletion
Task 2, Status RanToCompletion
Task 3, Status RanToCompletion
Task 4, Status RanToCompletion
Task 5, Status RanToCompletion
Task 6, Status RanToCompletion
Task 7, Status RanToCompletion
Task 8, Status RanToCompletion
Task 9, Status RanToCompletion
Task 10, Status RanToCompletion
Task 11, Status RanToCompletion
Task 12, Status RanToCompletion
Task 13, Status RanToCompletion
Task 14, Status RanToCompletion
Task 15, Status RanToCompletion
Task 16, Status RanToCompletion
Task 17, Status RanToCompletion
Task 18, Status RanToCompletion
Task 19, Status RanToCompletion
Task 20, Status RanToCompletion
Task 21, Status RanToCompletion
Task 22, Status RanToCompletion
Task 23, Status RanToCompletion
Task 24, Status RanToCompletion
Task 25, Status RanToCompletion
Task 26, Status RanToCompletion
Task 27, Status RanToCompletion
Task 28, Status RanToCompletion
Task 29, Status RanToCompletion

What is the reason of so many "empty spaces" and the extra tasks being ran for no reason and how to prevent it?

Any help is appreciated.

gafs
  • 73
  • 1
  • 7
  • 3
    Your textbox will contain newline characters, by taking each character you're including those as well, take each line at a time by using the `Lines` property. – hnefatl Oct 08 '17 at 11:57
  • Print `txtSearchTerms.Text[tmp].Trim()` – H H Oct 08 '17 at 12:02
  • To limit the taks (and to just improve this), use `Parallel.For()`. Optionally with a MaxDegree setting but I don't think you need that. – H H Oct 08 '17 at 12:03

2 Answers2

0

The issue with your code is that you are using txtSearchTerms.Text instead of txtSearchTerms.Lines.

As per https://stackoverflow.com/a/46630470/34092 consider using:

var data = txtSearchTerms.Lines;
var threadCount = 4; // or whatever you want

Parallel.ForEach(data, 
    new ParallelOptions() { MaxDegreeOfParallelism = threadCount },
    (val) =>
    {
        //Your code here
        Console.WriteLine(val);
    });

or:

var data = txtSearchTerms.Lines;
var threadCount = 4; // or whatever you want

var results = data.AsParallel(new ParallelLinqOptions()
{
    MaxDegreeOfParallelism = threadCount
}).Select(val =>
{
    // Your code here, I just return the value but you could return whatever you want
    return val;
}).ToList();
mjwills
  • 23,389
  • 6
  • 40
  • 63
-1

I think you have a task synch issue. The code does not appear to control the tasks in any way. It is possible a task runs while i changes. Maybe .run would be a better way to execute the tasks? Doesn't your goal call for running tasks synchronously? https://msdn.microsoft.com/en-us/library/dd321435(v=vs.110).aspx

This stackoverflow thread may be useful: Regarding usage of Task.Start() , Task.Run() and Task.Factory.StartNew()

Calin
  • 1