Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

GUI hangs up for several seconds when background work is finished

$
0
0

Hi!

I'm making a WinForms application in C# with Visual Studio 2015, targeting .NET 4.0.

I have a DataGridView on my only main form, whom DataSource is set to a BindingList in the form's constructor. The datatype is a simple Vector6 class I implemented, binding and notify works fine through INofityPropertyChanged.

    public class Vector6 : INotifyPropertyChanged
    {
        private double x, y, z;
        private double? xt, yt, zt;

        public double X { get { return x; } set { x = value; notifyPropertyChanged("X"); } }
        public double Y { get { return y; } set { y = value; notifyPropertyChanged("Y"); } }
        public double Z { get { return z; } set { z = value; notifyPropertyChanged("Z"); } }
        public double? Xt { get { return xt; } set { xt = value; notifyPropertyChanged("Xt"); } }
        public double? Yt { get { return yt; } set { yt = value; notifyPropertyChanged("Yt"); } }
        public double? Zt { get { return zt; } set { zt = value; notifyPropertyChanged("Zt"); } }

        public event PropertyChangedEventHandler PropertyChanged;

        public void ApplyTransformationMatrix(Matrix3x3 matrix)
        {
            Xt = X * matrix.A11 + Y * matrix.A21 + Z * matrix.A31;
            Yt = X * matrix.A12 + Y * matrix.A22 + Z * matrix.A32;
            Zt = X * matrix.A13 + Y * matrix.A23 + Z * matrix.A33;
        }

        public override string ToString()
        {
            return string.Format("{0} {1} {2} {3} {4} {5}", X, Y, Z, Xt, Yt, Zt);
        }

        private void notifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

I have a calculation, which fills the second 3 coordinates of the vectors based upon the first 3 and a matrix. This is started from a button's click event handler, but in a new task, and inside that, using Parallel.Foreach:

            computingTask = new Task(() =>
            {
                cancelButton.Enabled = true;
                try
                {
                    Parallel.ForEach(vectors, x =>
                    {
                        computingCT.ThrowIfCancellationRequested();
                        x.ApplyTransformationMatrix(_transformationMatrix);
                    });
                }
                catch(OperationCanceledException)
                {
                    MessageBox.Show("Cancelled");
                }
                progressBar.Style = ProgressBarStyle.Continuous;
                progressBar.MarqueeAnimationSpeed = 0;
                cancelButton.Enabled = false;
                MessageBox.Show("Finished");
            }, computingCT);

            computingTask.Start();

The code runs hell fast on all 8 cores, and I can see the datagrid filling with data continuously and without any freeze on the UI, until the calculation finishes. Then after I close the "Finished" message box, the GUI freezes for like 5 seconds. It does with 100.000 entries and 1. After that it responds again.

Edit: during the hang, task manager shows no CPU or IO activity for the process. Only the number of threads drops from 23 to 6 a little slowly.

I've run out of ideas why this happens.

Thanks for any help!



Viewing all articles
Browse latest Browse all 2535

Trending Articles