-3

I want to use Parallel.ForEach as a multi-threading method that executes a code without lists or file reading.

I want to do it like this:

Parallel.ForEach
{
      Console.WriteLine("test");
}

So it will write test without stopping. I will use an IF statement to check if it should be stopped or no.

Is it possible to use Parallel.ForEach like that?

Any help would be appreciated.

Thanks!

Mario
  • 1,374
  • 6
  • 22
  • 48
  • 1
    Why would you wanna use a `foreach` loop without anything to iterate on? what's the main goal here? `ForEach` seems irrelevant. – Selman Genç Aug 14 '18 at 21:51
  • @SelmanGenç - I want it to execute a normal code not iterated on a List. – Mario Aug 14 '18 at 21:52
  • Why do you want to use a construct to iterate over a list you don't have? Please explain how would you do it using a normal `foreach` – Camilo Terevinto Aug 14 '18 at 21:53
  • @NirmalSubedi - I want it to be multi-threaded. – Mario Aug 14 '18 at 21:58
  • OP, it makes no sense to repeat something forever in parallel. That sentence isn't even comprehensible to me. What are you trying to do overall? – John Wu Aug 14 '18 at 22:05
  • [`Parallel.Invoke`](https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-use-parallel-invoke-to-execute-parallel-operations)? – JSteward Aug 14 '18 at 22:06
  • @JohnWu - I am trying to write `test` for example more faster by multi-threading. – Mario Aug 14 '18 at 22:06
  • If you have a series of tests, wouldn't they be in a list, and wouldn't you iterate over that? If you don't have a series of tests but only a single test, again, I do not understand what your goal is here. – John Wu Aug 14 '18 at 22:07
  • 1
    Instead of checking some condition in an `if` statements (as you mentioned in your question), you can just use it as a condition for a loop: `while (someCondition == true) { Console.WriteLine("Test"); }` – Rufus L Aug 14 '18 at 22:32
  • `I want it to be multi-threaded.` **Why** do you want it multi-threaded? `Console.WriteLine` is going to use a locking mechanism anyway (i.e. will 'thwart' your attempts at multiple threads) - https://stackoverflow.com/questions/1079980/calling-console-writeline-from-multiple-threads . _If what you are trying to do is improve the performance of `Console.WriteLine` then reduce your number of calls to it. Don't write the string 100 times. Build a `string` which contains 100 lines of data and then call `Console.WriteLine` just once. It will be **way** quicker._ – mjwills Aug 14 '18 at 23:44

2 Answers2

1

Multi-threading this is not going to achieve anything, but if you insist:

bool _shouldStop { get; set; } = false;
bool _shouldStopSecondThread { get; set; } = false;

public static Main(string[] args)
{

Thread thread = new Thread(print);
Thread anotherThread = new Thread(anotherPrint);
thread.Start();
anotherThread.Start();

//your code here while the worker writes "Test" to console

if(condition) {
   _shouldStop=true;
   _shouldStopSecondThread=false; 
}
}    
//...

public void print()
{
   while (!_shouldStop)
   {
       Console.WriteLine("test");
   }
   Console.WriteLine("worker terminated");
}

public void anotherPrint()
{
   while (!_shouldStopSecondThread)
   {
       Console.WriteLine("test");
   }
   Console.WriteLine("worker terminated");
}
Daniel Loudon
  • 799
  • 3
  • 18
0

What you want is not very clear but you may try this approach:

        var parallelDrege = Environment.ProcessorCount;
        while (true)
        {
            Parallel.For(0, parallelDrege, t =>
            {
                Console.WriteLine("test");
            });
        }

Set parallelDegre to the desired value and be aware that every batch will be processed in parallel but from batch to batch is a sequetial process.

Hope this help!

Miguel
  • 3,786
  • 2
  • 19
  • 32