-1

I have a code which basically looks like this:

Parallel.Foreach(items,dbItem=>
{
  int counter = 0; 
  Interlocked.Increment(ref counter);
  Console.WriteLine("Current counter state: " + counter);
});

What I would like to do here is to simple re-print the counter value for specific thread that is being fired up like this:

Thread {0} - Current conter state: 1 << Value 1 gets re-printed every time this console.writeline occurs... ? 
Thread {1} - Current conter state: 1
Thread {2} - Current conter state: 1
Thread {3} - Current conter state: 1
Thread {4} - Current conter state: 1
Thread {5} - Current conter state: 1

Is this doable, if so how ?

P.S. What I ment by the value being re-printed is to show the current value of counter on each thread without doing a new console.writeline and spamming my console with bunch of same text...

P.S. 2 ,guys this is the desired output:

   Thread {1} - Current conter state: >>2<< this value here gets updated

And without having to do something like this in console:

   Thread {1} - Current conter state: 1
   Thread {1} - Current conter state: 2
   Thread {1} - Current conter state: 3
   Thread {1} - Current conter state: 4
   Thread {1} - Current conter state: 5

I would simply like to re-print the counter value in Console application...

User987
  • 3,663
  • 15
  • 54
  • 115
  • 4
    You are declaring counter inside the lambda that gets executed, that's why you are always getting 1. If you want to share `counter` then declare it outside the lambda. – Ed T Aug 07 '17 at 19:10
  • @EdT I don't want to share the counter value.. It's gonna be unique for each thread.. But is there any way to reprint the value without adding the new line of text allover again ? – User987 Aug 07 '17 at 19:11
  • Are you looking for `Console.SetCursorPosition`? – Aleks Andreev Aug 07 '17 at 19:13
  • For example counter on thread 1 will have value 2,3,4,5,6 and counter on thread 4 is gonna have value 16,17,18... – User987 Aug 07 '17 at 19:13
  • @AleksAndreev I'm not really sure , this is the first time I'm facing tis issue, could you post a reply so that I can see what this does exactly? – User987 Aug 07 '17 at 19:13
  • @AleksAndreev can you look into my updated question ? – User987 Aug 07 '17 at 19:15

1 Answers1

0

So you need update a value in console? Try this code (it's just a demo):

var counters = new ConcurrentDictionary<string, int>();

var threads = Enumerable.Range(0, 5).Select(i => new Thread(() =>
{
    var key = $"Thread {i}";
    for (var j = 0; j < 10; ++j)
    {
        var counter = j;
        counters.AddOrUpdate(key, counter, (k, v) => counter);
        Thread.Sleep(200);
    }
})).ToList();
threads.ForEach(t => t.Start());

while (threads.Any(t => t.IsAlive))
{
    int line = 0;
    foreach (var counter in counters)
    {
        Console.SetCursorPosition(1, line++);
        Console.WriteLine($"{counter.Key} - {counter.Value}");
        Thread.Sleep(50);
    }
}
Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
  • Hey Aleks thanks for the reply... This sorta works? I need to make it work in parallel foreach loop with multiple.. The value that I get printed out right now is counter value from multiple threads... And I'd like to print out separately each value of each counter which are on separate threads xD – User987 Aug 07 '17 at 19:29
  • @User987 yes it works for single thread scenario. I've posted this just like an example how you can update text already printed to console. – Aleks Andreev Aug 07 '17 at 19:38
  • Aleks thanks a lot for that, but I'm looking this exactly , but for the multi-threaded scenario ^^ – User987 Aug 07 '17 at 19:39