goblinfactory / konsole

Home of the simple console library consisting of ProgressBar, Window, Form, Draw & MockConsole (C# console progress bar with support for single or multithreaded progress updates) Window is a 100%-ish console compatible window, supporting all normal console writing to a windowed section of the screen, supporting scrolling and clipping of console output.
721 stars 62 forks source link

Concole Progress Bar is not thread safe #1

Closed Darida closed 7 years ago

Darida commented 7 years ago

The refresh method has lock but constructor doesn't. So when threads creating ProgressBar inside or the one bar is already start reporting in other thread, the output overrides each other.

goblinfactory commented 7 years ago

ooh, nice find! looking at this now, thank you for the feedback...

goblinfactory commented 7 years ago

Hi @Darida ;

If you can please give me a small sample of it breaking then I can write an acceptance test to add this to the code and fix it.

I believe the workaround for this (for now) is to create all the progress bars using a single thread and pass the 'created' progress bars to your threads doing any processing as a parameter, rather than having each separate thread create their own progress bar.

I can't confirm this workaround will work for you without a bit of sample code. A small sample will also help me make sure the fix I'm about to commit will definitely work for you.

For an example on multiple threads accessing ProgressBar checkout ProgressBarDemos.cs

If you want to play with the examples I've written, the solution is really easy to check-out and build.

git clone https://github.com/goblinfactory/konsole.git

After cloning the project, right click on Konsole.Sample project and set as startup project, and press F5, ...you'll get a menu with demos (see below) , press 'l' for the parallel thread demo.

screen shot 2017-03-20 at 11 49 58

Thanks, Alan

goblinfactory commented 7 years ago

@Darida No worries, I have a test that reproduces the problem; fix is on the way...

goblinfactory commented 7 years ago

fix and test committed. will push a new package a bit later. Here's test proving fix works

goblinfactory commented 7 years ago

I will close issue as soon as new package has been published to nuget.