Question

I have the next code, and works fine:

private void BtBasicIntroClick(object sender, EventArgs e)
    {
        var stopwatch = new Stopwatch();
        stopwatch.Reset();
        stopwatch.Start();
        var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions
                {
                    //TaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(),
                    MaxDegreeOfParallelism = 1
                };
        var actionBlock = new ActionBlock<int>(s =>
                                                   {
                                                       //comboBox1.Items.Add((s*3).ToString());
                                                       Invoke(new Action(() => comboBox1.Items.Add((s * 3).ToString())));
                                                   }, executionDataflowBlockOptions);
        var numeros = Enumerable.Range(0, 40000);
        foreach (var numero in numeros)
        {
            actionBlock.Post(numero);
        }
        Task.Factory.StartNew(() =>
                                  {

                                      actionBlock.Completion.ContinueWith(delegate
                                                            {
                                                                stopwatch.Stop();
                                                                if (InvokeRequired)
                                                                {
                                                                    Invoke(new Action(() => 
                                                                        label1.Text = stopwatch.ElapsedMilliseconds.ToString(CultureInfo.InvariantCulture)));
                                                                }
                                                            });
                                      actionBlock.Complete();
                                      actionBlock.Completion.Wait();
                                  });
    }

The Windows Forms works fine... the proccess doesn't block the UI

But if I change the MaxDgreeOfParallelism to other value (2 or 3 or 4...) the UI is blocked until the proccess finish.

I've see the Parallel Tasks window and the Thread Window in Visual Studio and in both cases everything works in Worked Threads, but in the latter case (When the MaxDgreeOfParallelism it's different from 1 ) the UI don't respond until the proccess finish

Why?

Was it helpful?

Solution

When I try your code, the UI is blocked the whole time, even with MaxDegreeOfParallelism = 1. That's because all your block does is to call Invoke(), which blocks the UI thread.

It's possible that under some circumstances, one thread calling Invoke() over and over won't be enough to block the UI thread completely, but 2 threads almost certainly will.

So, what you're trying to do doesn't make any sense. You're not going to gain anything from using dataflow, or anything similar.

What you should do to fix this is not to have thousands of items in the UI. No human is going to go though such huge list anyway.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top