bobbyalex
bobbyalex

Reputation: 2751

How do you update a datagrid (or any other UI control) continuously without freezing the UI?

In a WinForms application, I have a datagrid that is associated to a datasource. As and when data comes in through a background thread the dataset needs to be updated which in turn automatically updates the datagrid. Now, the updates can be in the order of say, 7000 updates per 20 seconds. The probelm is that the UI hangs when such an update happens because it has to happen on the main thread. Is there a know solution for this problem?

Generally, how can you design highly performant enterprise applications in WinForms where the UI is being updated continuously without the application freezing?


Adding a scenario to explain this:

Consider this scenario. You have a tree view which you are using to represent some hierarchical data. Now the data update on the tree is async. The server can publish one or 1000 updates at the same same time. The update can be a modification of an existing item or addition of new nodes. The point to be noted is that the update cannot be delayed. The nodes represent a real time entity somewhere. Delaying the update will give the user the perception that the event itself was delayed. So this cant be done. If it was possible (from the business logic point of view) I would have done it a long time back.

There is a key point here: All the data need not be visible at the same time.

So that people dont suggest this anymore:

Adding a background worker thread WILL NOT HELP because the thread has to switch to the main thread to perform the update. The worker thread will make no difference.

Upvotes: 9

Views: 5714

Answers (14)

Sharique
Sharique

Reputation: 4219

There is an article posted on MSDN regarding Asynchronous Call Pattern for Windows Forms. I hope this helps.

Upvotes: 0

Qorbani
Qorbani

Reputation: 5905

I recommend to use two layers for handling this scenario.

Non-UI Data Layer:

This layer can grab all updates from your background thread and generate final Data object (Let's call it ObjectX) which is the latest and most current state of data. This layer should run on it's own thread which will not effect UI at all. Also after receiving any update you can have one boolean variable (Let's call it NewUpdateExist) and set it to true which indicates that new changes has been received. Don't forget to use Thread-safe locking mechanism to set this variable to True, to avoid concurrency issues.

UI Sync Layer:

This layer also can run on separated thread. You can have a timer which will trigger on specific interval(*) to checks if there is any new data since last UI update by checking out NewUpdateExist variable and if there is, then Set NewUpdateExist to false and generate new sub-set of data which only required to display on Screen (***). Don't forget to use Thread-safe locking while generating sub-set of data and updating NewUpdateExist variable.

After generating your sub-set of data then you need to Invoke another method by control (In UI Thread) to apply this sub-set of data to the control. This is where UI thread will block until process done so you need to make this method as light as possible! All heavy stuff needs to be done outside of Invoke and only part that relate to UI control needs to be in that method.

(*) As "Hans Passant" mentioned in his comment, human eye only can process 50 milliseconds refresh, but I even suggest to increase this to 100 msec. You can get some ideas from this thread: What is the shortest perceivable application response delay?

(**) Tricky part in this case is how to update your control with only data that is required instead of pushing all data at once to the UI. I really recommend to implement custom controls to handle this part instead of using standard controls; because you'll have full access to how and when to update UI and you can achieve best performance. For example on Grid you can find out first visible item and number of items that can be displayed on UI and just update that portion instead of trying to update control with all data.

Sorry, I know I suppose to explain solution in short message but this is the shortest version that I can came across. I hope this helps :-)

Upvotes: 0

Beth
Beth

Reputation: 9617

re: The problem is that the UI hangs when such an update happens because it has to happen on the main thread. Is there a known solution for this problem?

no, as you're seeing

Generally, how can you design highly performant enterprise applications in WinForms where the UI is being updated continuously without the application freezing?

At the scale you're describing, you can't. Try limiting the scope of your UI control so it doesn't try to display everything happening everywhere at once, but forces the user to pick an area to focus on that you can update at an acceptable rate.

Upvotes: 0

Arthur Stankevich
Arthur Stankevich

Reputation: 382

Is the bottleneck in processing data from server or in actually putting it to DataGridView? If latter, VirtualMode can help you: http://msdn.microsoft.com/en-us/library/2b177d6d.aspx.

Upvotes: 1

Kevin McCormick
Kevin McCormick

Reputation: 2398

You cannot, unless you want to use DirectX.

Windows Forms is not designed for real-time information display. As many others have pointed out, you can get very close, but due to the way the Windows message loop works, you absolutely cannot guarantee that what is on the screen will be "real-time", even if you create a Timer that ticks at 60hz. Even if you do this in an event-driven manner, Windows will still queue up a WM_PAINT message that will inevitably be late if you are looking for real-time display.

If you truly want a extremely-close-to-realtime display, you will need to implement something similar to a Game Loop.

For an explanation of why the Windows Message loop won't work for real-time display, and what a game loop is, see: http://www.mvps.org/directx/articles/writing_the_game_loop.htm

A computer game cannot have any perceivable delay, so most computer games try to optimize performance so that they approach a framerate at or above the holy grail of 60hz. (Movies are only projected at 24hz, do you perceive them to be "delayed"?)

Writing an application with a real-time display is non-trivial and I would highly suggest considering compromising with what Windows provides in any of the following ways:

  • Create a timer that queues up screen updates at an acceptable rate (10 or more times per second). The user will not perceive the event as delayed, because a user cannot perceive delays that occur at a small fraction of a second.
  • Raise an event when the underlying data changes and let Windows to decide when to update the display (this will virtually always be acceptable).
  • If possible, come up with an alternate display that is not grid-based. Perhaps a scrolling console, or some other interface that displays the relevant information without overwriting old information. This may not be applicable, but coming up with another interface idea is often a good approach when the interface you want won't work.

If you really, really still want a very high-performance user interface, and write a game loop, you can do so in C# and paint a grid on your own to a DirectX surface. Once you get the hang of DirectX, drawing a grid is fairly easy, it's just a bunch of lines. With this approach you will avoid dealing with the Windows message loop, and potentially approach real-time performance.

Here's a great tutorial on how to use DirectX and how to render on a Windows form:

http://www.godpatterns.com/2005/02/using-directx-and-c-sharp-to-create.html

Upvotes: 7

Slugart
Slugart

Reputation: 4680

One solution to this problem is to update your data model periodically, i.e. to batch update them from the communication thread every x milliseconds. A little more information on how you are accessing the server data would be helpful in giving you a more explicit suggestion.

At the same time you should be using virtualised controls (especially the datagrid). Using a virtual grid basically means visible cells are rendered on the fly. Hence you only need to update the data that is currently displayed. As each cell becomes visible it will access the data model to obtain the relevent value at that time. See this link as a starting point to implementing a virtual grid.

By combining these two approaches you should be able to minimise the amount of updates to the grid.

Upvotes: 0

Jacek Gorgoń
Jacek Gorgoń

Reputation: 3214

In your comment you say that your heavy processing is reporting progress very often and you can't drop any report (because the report is real data that needs showing).

What you should do is implement (double) buffering, report progress to a buffer and only synchronize the buffer with GUI every once in a while.

Pseudocode follows:

DataGrid Grid; // This displays the data
List<object> DataBuffer; // Frequent updates are performed on this list

void BackgroundThreadLoop()
{
   while(true) // This loop iterates 7000 times in 20 seconds
   {
       var result = DoSomeHeavyCalculations();

       // Depending on the nature of the result, you can either just add it to list
       // or perhaps modify existing entries in the list in some way.
       DataBuffer.Add(result); // The simple case
       PerformSomeUpdating(DataBuffer, result); // The complicated case
   }
}

Timer RefreshTimer;
override void OnLoad()
{
    RefreshTimer = new Timer();
    RefreshTimer.Interval = 500; // easy to experiment with this
    RefreshTimer.Tick += (s, ea) => DrawBuffer(DataBuffer);
}

void DrawBuffer(List<object> DataBuffer)
{
    // This should copy DataBuffer and put it in the grid as fast as possible.

    // How to do this really depends on how the list changes and what it contains.
    // If it's just a list of strings:
    Grid.DataSource = DataBuffer.ToList(); // Shallow copy is OK with strings

    // If it's a list of some objects that have meaningful Clone method:
    Grid.DataSource = DataBuffer.Select(o => o.Clone).ToList();

    // If the number of elements is like constant and only some values change,
    // you could use some Dictionary instead of List and just copy values.
}

If you give more precise info, I might be able to help further.

UPDATE

With the new details, I'd suggest buffering individual changes made to objects. The most general way of representing a change to some objects structure would be a function (perhaps parameterless Action). While receiving changes, you construct the update-functions modifying directly the view-bound data and store them in the buffer:

List<Action> UpdateBuffer;
void OnUpdateReceived(MyType objToModify, object newValue)
{
    // The point is to make the lambda (below) as efficient as you can; 
    // finding the object and preparing the update should be done here, so that
    // no time is wasted during redraw in the main thread.

    UpdateBuffer.Add(() => objToModify.ApplyNewValueInSomeWay(newValue));


    // some other method should be constructed to add data to the view, but you get the point
}

Now the DrawBuffer (name no longer fully adequate, but no matter) method would be easy:

void DrawBuffer()
{
    List<Action> bufferCopy;
    lock(UpdateBuffer) // the other thread should also lock the buffer for adding
    {
        bufferCopy = UpdateBuffer.ToList();
        UpdateBuffer.Clear();
    }
    view.SuspendLayout();
    foreach(Action a in bufferCopy)
        a();
    view.ResumeLayout();
}

Obviously I have not tried this exact solution, but it gives you the ability to control redraw frequency and redraw whole batches instead of single updates.

Upvotes: 5

MartinS
MartinS

Reputation: 751

Use backgroundWorker, it will run inserted code in separated thread, so the application will not freeze.

 public void backgroundWorkerPinger_DoWork(object sender, DoWorkEventArgs e)
        {

            Ping ping = new Ping();

                try
                {
                    PingReply pingreply = ping.Send("46.4.106.10", 500);
                    string active = pingreply.Status.ToString();
                    if (active == "Success")
                    {
                        //Pokud je spojení aktivni pak se nastavi barva labelu na zelenou a vypise se aktivni
                        ActiveOrNotLabel.ForeColor = Color.Green;
                        ActiveOrNotLabel.Text = "Aktivní";
                       // MessageBox.Show("vyjimka2");
                        if (connection_enabled == false)
                        {
                            admini.Enabled = true;
                            connection_enabled = true;
                        }
                    }
                    if (active != "Success") {
                        ActiveOrNotLabel.ForeColor = Color.Red;
                        ActiveOrNotLabel.Text = "Neaktivní";
                        admini.Enabled = false;
                        connection_enabled = false;

                     }
                }
                catch
                {
                    //Jinak na cervenou a neaktivni

                    //MessageBox.Show("vyjimka");
                    ActiveOrNotLabel.ForeColor = Color.Red;
                    ActiveOrNotLabel.Text = "Neaktivní";
                    admini.Enabled = false;
                    connection_enabled = false;

                }
            }

Upvotes: -1

allen.mn
allen.mn

Reputation: 487

I've done a lot of high volume data transfers (hundreds per second) like this and I think a DataGrid just isn't the control you want. It's designed to present data and let the user edit it, it's not really optimized to be an information stream. At this volume it won't do a user much good to view the data in real time, it's just going to be a stream of data too large and fast to make sense of.

I suggest you continue to use a background worker to do the work (like you said you are) and use the ReportProgress method to send a % done back to a progress bar. You can also update a label on the page with the file you're working on. The label will update automatically and not freeze your UI. To do this, create an instance variable in the class your background worker calls. On your UI, create an instance of that class and in the background worker's ProgressChanged method set your UI label to your class instance variable. It will update each time you call backgroundworker.ReportProgress()

Then put all the info in a log so someone can look at it later. It's just not that beneficial to try to visually take in 350 changes/second.

Upvotes: 0

Farid Movsumov
Farid Movsumov

Reputation: 12735

Application.DoEvents();

Use this method inside timer.

private void timer1_Tick(object sender, EventArgs e)
{
     Application.DoEvents();
}

You should start timer where your UI freezes or you can start it in form_Load and set small number to your timer's interval to make it ticks frequently. For Example set it to ten.

timer1.Start();
timer1.Interval = 10;

Upvotes: 0

Oliver
Oliver

Reputation: 45109

I just made an sample application, that will fill its internal list through a BackgroundWorker and the data will be displayed within a DataGridView. You can change the speed of the inserts to find out if it meets your requirements:

The most interesting part should be the code within the form itself:

public partial class FormMain : Form
{
    private List<Person> _Persons;
    private Random _Random;
    private int _TimeoutBetweenInserts;

    public FormMain()
    {
        InitializeComponent();

        // Initialize our private fields
        _Random = new Random();
        _Persons = new List<Person>();
        _TimeoutBetweenInserts = (int)numericUpDownTimeoutBetweenInserts.Value;

        // Attach the list to the binding source and get informed on list changes.
        personBindingSource.DataSource = _Persons;
        personBindingSource.ListChanged += (sender, e) => labelDataGridViewCount.Text = _Persons.Count.ToString();
    }

    private void OnBackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
    {
        var spinner = new SpinWait();
        var worker = (BackgroundWorker)sender;

        // Should we abort our adding?
        while (!worker.CancellationPending)
        {
            // Create a new entry ...
            var person = new Person();

            person.Index = _Persons.Count;
            person.Born = new DateTime(_Random.Next(1950, 2012), _Random.Next(1, 13), _Random.Next(1, 28));
            person.FirstName = "Hello";
            person.LastName = "World";

            // ... and add it to the list
            _Persons.Add(person);

            // Do a little waiting ... (to avoid blowing out the list)
            for (int i = 0; i < _TimeoutBetweenInserts; i++)
            {
                spinner.SpinOnce();
            }

            spinner.Reset();
        }

    }

    private void OnBackgroundWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        // Stop the gui updater, cause the background worker also stopped.
        timerGuiUpdater.Stop();
    }

    private void OnCheckBoxToggleWorkerCheckedChanged(object sender, EventArgs e)
    {
        // Update the "button" according to the state
        checkBoxToggleWorker.Text = checkBoxToggleWorker.Checked ? "&Pause" : "&Start";

        if (checkBoxToggleWorker.Checked)
        {
            if (!backgroundWorker.IsBusy)
            {
                // Start the gui updater and the background worker
                timerGuiUpdater.Start();
                backgroundWorker.RunWorkerAsync();
            }
        }
        else
        {
            // Stop the background worker
            backgroundWorker.CancelAsync();
        }
    }

    private void OnNumericUpDownTimeoutBetweenInsertsValueChanged(object sender, EventArgs e)
    {
        // Update the internal value, to let it propagate into the background worker
        _TimeoutBetweenInserts = (int)numericUpDownTimeoutBetweenInserts.Value;
    }

    private void OnTimerGuiUpdaterTick(object sender, EventArgs e)
    {
        // Tell the BindingSource it should inform its clients (the DataGridView)
        // to update itself
        personBindingSource.ResetBindings(false);
    }
}

To let you access all these fields within the form, here comes the Designer.cs:

partial class FormMain
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.dataGridView = new System.Windows.Forms.DataGridView();
        this.Index = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.lastNameDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.firstNameDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.bornDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.ageDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.personBindingSource = new System.Windows.Forms.BindingSource(this.components);
        this.backgroundWorker = new System.ComponentModel.BackgroundWorker();
        this.labelDataGridViewCountText = new System.Windows.Forms.Label();
        this.labelDataGridViewCount = new System.Windows.Forms.Label();
        this.labelSpinsBetweenInsertsText = new System.Windows.Forms.Label();
        this.numericUpDownTimeoutBetweenInserts = new System.Windows.Forms.NumericUpDown();
        this.checkBoxToggleWorker = new System.Windows.Forms.CheckBox();
        this.timerGuiUpdater = new System.Windows.Forms.Timer(this.components);
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.personBindingSource)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.numericUpDownTimeoutBetweenInserts)).BeginInit();
        this.SuspendLayout();
        // 
        // dataGridView
        // 
        this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.dataGridView.AutoGenerateColumns = false;
        this.dataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
        this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.Index,
        this.lastNameDataGridViewTextBoxColumn,
        this.firstNameDataGridViewTextBoxColumn,
        this.bornDataGridViewTextBoxColumn,
        this.ageDataGridViewTextBoxColumn});
        this.dataGridView.DataSource = this.personBindingSource;
        this.dataGridView.Location = new System.Drawing.Point(12, 12);
        this.dataGridView.Name = "dataGridView";
        this.dataGridView.Size = new System.Drawing.Size(560, 212);
        this.dataGridView.TabIndex = 0;
        // 
        // Index
        // 
        this.Index.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
        this.Index.DataPropertyName = "Index";
        this.Index.HeaderText = "Index";
        this.Index.Name = "Index";
        this.Index.Width = 58;
        // 
        // lastNameDataGridViewTextBoxColumn
        // 
        this.lastNameDataGridViewTextBoxColumn.DataPropertyName = "LastName";
        this.lastNameDataGridViewTextBoxColumn.HeaderText = "LastName";
        this.lastNameDataGridViewTextBoxColumn.Name = "lastNameDataGridViewTextBoxColumn";
        // 
        // firstNameDataGridViewTextBoxColumn
        // 
        this.firstNameDataGridViewTextBoxColumn.DataPropertyName = "FirstName";
        this.firstNameDataGridViewTextBoxColumn.HeaderText = "FirstName";
        this.firstNameDataGridViewTextBoxColumn.Name = "firstNameDataGridViewTextBoxColumn";
        // 
        // bornDataGridViewTextBoxColumn
        // 
        this.bornDataGridViewTextBoxColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
        this.bornDataGridViewTextBoxColumn.DataPropertyName = "Born";
        this.bornDataGridViewTextBoxColumn.HeaderText = "Born";
        this.bornDataGridViewTextBoxColumn.Name = "bornDataGridViewTextBoxColumn";
        this.bornDataGridViewTextBoxColumn.Width = 54;
        // 
        // ageDataGridViewTextBoxColumn
        // 
        this.ageDataGridViewTextBoxColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
        this.ageDataGridViewTextBoxColumn.DataPropertyName = "Age";
        this.ageDataGridViewTextBoxColumn.HeaderText = "Age";
        this.ageDataGridViewTextBoxColumn.Name = "ageDataGridViewTextBoxColumn";
        this.ageDataGridViewTextBoxColumn.ReadOnly = true;
        this.ageDataGridViewTextBoxColumn.Width = 51;
        // 
        // personBindingSource
        // 
        this.personBindingSource.DataSource = typeof(WindowsFormsApplication.Person);
        // 
        // backgroundWorker
        // 
        this.backgroundWorker.WorkerSupportsCancellation = true;
        this.backgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.OnBackgroundWorkerDoWork);
        this.backgroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.OnBackgroundWorkerRunWorkerCompleted);
        // 
        // labelDataGridViewCountText
        // 
        this.labelDataGridViewCountText.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
        this.labelDataGridViewCountText.Location = new System.Drawing.Point(12, 230);
        this.labelDataGridViewCountText.Name = "labelDataGridViewCountText";
        this.labelDataGridViewCountText.Size = new System.Drawing.Size(50, 23);
        this.labelDataGridViewCountText.TabIndex = 1;
        this.labelDataGridViewCountText.Text = "Count:";
        this.labelDataGridViewCountText.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
        // 
        // labelDataGridViewCount
        // 
        this.labelDataGridViewCount.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
        this.labelDataGridViewCount.Location = new System.Drawing.Point(68, 230);
        this.labelDataGridViewCount.Name = "labelDataGridViewCount";
        this.labelDataGridViewCount.Size = new System.Drawing.Size(82, 23);
        this.labelDataGridViewCount.TabIndex = 2;
        this.labelDataGridViewCount.Text = "0";
        this.labelDataGridViewCount.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        // 
        // labelSpinsBetweenInsertsText
        // 
        this.labelSpinsBetweenInsertsText.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.labelSpinsBetweenInsertsText.Location = new System.Drawing.Point(265, 230);
        this.labelSpinsBetweenInsertsText.Name = "labelSpinsBetweenInsertsText";
        this.labelSpinsBetweenInsertsText.Size = new System.Drawing.Size(155, 23);
        this.labelSpinsBetweenInsertsText.TabIndex = 3;
        this.labelSpinsBetweenInsertsText.Text = "Spins between inserts:";
        this.labelSpinsBetweenInsertsText.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
        // 
        // numericUpDownTimeoutBetweenInserts
        // 
        this.numericUpDownTimeoutBetweenInserts.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.numericUpDownTimeoutBetweenInserts.Increment = new decimal(new int[] {
        10,
        0,
        0,
        0});
        this.numericUpDownTimeoutBetweenInserts.Location = new System.Drawing.Point(426, 233);
        this.numericUpDownTimeoutBetweenInserts.Maximum = new decimal(new int[] {
        500,
        0,
        0,
        0});
        this.numericUpDownTimeoutBetweenInserts.Minimum = new decimal(new int[] {
        10,
        0,
        0,
        0});
        this.numericUpDownTimeoutBetweenInserts.Name = "numericUpDownTimeoutBetweenInserts";
        this.numericUpDownTimeoutBetweenInserts.Size = new System.Drawing.Size(65, 20);
        this.numericUpDownTimeoutBetweenInserts.TabIndex = 4;
        this.numericUpDownTimeoutBetweenInserts.ThousandsSeparator = true;
        this.numericUpDownTimeoutBetweenInserts.Value = new decimal(new int[] {
        500,
        0,
        0,
        0});
        this.numericUpDownTimeoutBetweenInserts.ValueChanged += new System.EventHandler(this.OnNumericUpDownTimeoutBetweenInsertsValueChanged);
        // 
        // checkBoxToggleWorker
        // 
        this.checkBoxToggleWorker.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.checkBoxToggleWorker.Appearance = System.Windows.Forms.Appearance.Button;
        this.checkBoxToggleWorker.Location = new System.Drawing.Point(497, 230);
        this.checkBoxToggleWorker.Name = "checkBoxToggleWorker";
        this.checkBoxToggleWorker.Size = new System.Drawing.Size(75, 23);
        this.checkBoxToggleWorker.TabIndex = 6;
        this.checkBoxToggleWorker.Text = "&Start";
        this.checkBoxToggleWorker.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        this.checkBoxToggleWorker.UseVisualStyleBackColor = true;
        this.checkBoxToggleWorker.CheckedChanged += new System.EventHandler(this.OnCheckBoxToggleWorkerCheckedChanged);
        // 
        // timerGuiUpdater
        // 
        this.timerGuiUpdater.Tick += new System.EventHandler(this.OnTimerGuiUpdaterTick);
        // 
        // FormMain
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(584, 262);
        this.Controls.Add(this.checkBoxToggleWorker);
        this.Controls.Add(this.numericUpDownTimeoutBetweenInserts);
        this.Controls.Add(this.labelSpinsBetweenInsertsText);
        this.Controls.Add(this.labelDataGridViewCount);
        this.Controls.Add(this.labelDataGridViewCountText);
        this.Controls.Add(this.dataGridView);
        this.MinimumSize = new System.Drawing.Size(600, 300);
        this.Name = "FormMain";
        this.Text = "DataGridView Performance Tester";
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.personBindingSource)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.numericUpDownTimeoutBetweenInserts)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.DataGridView dataGridView;
    private System.ComponentModel.BackgroundWorker backgroundWorker;
    private System.Windows.Forms.BindingSource personBindingSource;
    private System.Windows.Forms.Label labelDataGridViewCountText;
    private System.Windows.Forms.Label labelDataGridViewCount;
    private System.Windows.Forms.Label labelSpinsBetweenInsertsText;
    private System.Windows.Forms.NumericUpDown numericUpDownTimeoutBetweenInserts;
    private System.Windows.Forms.CheckBox checkBoxToggleWorker;
    private System.Windows.Forms.Timer timerGuiUpdater;
    private System.Windows.Forms.DataGridViewTextBoxColumn Index;
    private System.Windows.Forms.DataGridViewTextBoxColumn lastNameDataGridViewTextBoxColumn;
    private System.Windows.Forms.DataGridViewTextBoxColumn firstNameDataGridViewTextBoxColumn;
    private System.Windows.Forms.DataGridViewTextBoxColumn bornDataGridViewTextBoxColumn;
    private System.Windows.Forms.DataGridViewTextBoxColumn ageDataGridViewTextBoxColumn;
}

Last but not least my little person class that is used for the payload:

public class Person
{
    public int Age
    {
        get
        {
            // ToDo: better algorithm to determine real age is left as an exercise to the reader. ;-)
            var age = (int)((DateTime.Now - Born).TotalDays / 365);
            return Math.Max(0, age);
        }
    }

    public DateTime Born { get; set; }

    public string FirstName { get; set; }

    public int Index { get; set; }

    public string LastName { get; set; }
}

Upvotes: 2

Emond
Emond

Reputation: 50682

The UI will always be updated by the main/UI thread. That is the way WinForms works.

What you can do is prevent the UI-thread from doing too much. To do that:

  1. Make sure you execute all other processing on one or more different threads.
  2. Only update the UI when useful for the user. I can't see/read a number that is changing every 3 ms so skip DISPLAYING the update.

Note that I use the terms ViewModel, View and Model is the remainder of this answer. I am not forcing you to use MVVM but it makes explaining things easier. You could use MVP or MVC in the same way.

You could create a special kind of ViewModel that raises an event after x milliseconds to check for a 'dirty bits' and raise appropriate PropertyChanged events. This would require you to set the dirty bits in the property setters and NOT raise the PropertyChanged event in the setters.

Perhaps even better might be to keep track of the last time a ViewModel was updated; when it is longer than x milliseconds ago, update the ViewModel from the Model, otherwise don't. This guarantees the UI to be in-sync with the ViewModel. But you have to realize that the ViewModel is not in-sync with the Model. Of course it is possible to create methods to sync the models explicitly.

The choice between these two might depend on how you think about the View-ViewModel relation and how much time this all is costing.

Upvotes: 1

Amar Palsapure
Amar Palsapure

Reputation: 9680

You can do this using BackgroundWorker. In the DoWork method you can iterate update the datagrid.

To update datagrid from Non-UI thread you will need to as follows

  1. Create an extension method like

    public static class ControlExtensions
    {
        public static void Invoke(this Control control, Action action)
        {
          if (control.InvokeRequired) control.Invoke(new MethodInvoker(action), null);
          else action.Invoke();
        }
    }
    
  2. Update data grid as (assuming dataGrid is your control id and dataSource is your data source)

    dataGrid.Invoke(() => { dataGrid.DataSource = dataSource; };
    

Hope this works for you.

Upvotes: 1

Deitools
Deitools

Reputation: 431

Are you using a BackgroundWorker? Put the code that makes you application to freeze in DoWork event:

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
 {
     YourFreezingCodeHere
 }

And start backgroundWorker like

backgroundWorker1.RunWorkerAsync();

Upvotes: 1

Related Questions