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

ProgressBar worked parallel example is not working #25

Closed sekulicb closed 5 years ago

sekulicb commented 5 years ago

ProgressBar worked parallel example needs an update, looks like ConcurrentQueue has no Dequeue method anymore, just TryeDequeue. Thanks

goblinfactory commented 5 years ago

Can you please be more specific? Do you mean that some functionality no longer works, or that a specific test no longer works? If so, which test are you referring to? Please give the name of the test that fails, and the name of the file the test is in so that I can take a look.

sekulicb commented 5 years ago

Download nuget, copy paste ProgressBar worked parallel example and the line

var files = q.Dequeue(d.cnt).ToArray();

could not be resolved. Do we need another nuget package ?

''' using Konsole;

Console.WriteLine("ready press enter.");
Console.ReadLine();

var dirCnt = 15;
var filesPerDir = 100;
var r = new Random();
var q = new ConcurrentQueue<string>();
foreach (var name in TestData.MakeNames(2000)) q.Enqueue(name);
var dirs = TestData.MakeObjectNames(dirCnt).Select(dir => new
{
    name = dir,
    cnt = r.Next(filesPerDir)
});

var tasks = new List<Task>();
var bars = new ConcurrentBag<ProgressBar>();
foreach (var d in dirs)
{
    var files = q.Dequeue(d.cnt).ToArray();  <-- this line here --
    if (files.Length == 0) continue;
    tasks.Add(new Task(() =>
    {
        var bar = new ProgressBar(files.Count());
        bars.Add(bar);
        bar.Refresh(0, d.name);
        ProcessFakeFiles(d.name, files, bar);
    }));
}

foreach (var t in tasks) t.Start();
Task.WaitAll(tasks.ToArray());
Console.WriteLine("done.");

'''

Thanks

goblinfactory commented 5 years ago

Hi @sekulicb you've cut and pasted only a small part of the sample project. I suggest you clone the whole sample project and then look at all the code in the sample project.

The code you have copied comes from ProgressBarDemos.cs in Konsole.Sample project. You've copied lines 54 to 88 and you did not copy the rest of the file that includes an extension method helper at lines 15 to 28.

        public static IEnumerable<T> Dequeue<T>(this ConcurrentQueue<T> src, int x)
        {
            int cnt = 0;
            bool more = true;
            while (more && cnt<x)
            {
                T item;
                more = src.TryDequeue(out item);
                cnt++;
                yield return item;
            }
        }

Copy the whole sample project and use that to take a look around. . :D easy mistake.