• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1407
  • Last Modified:

Using Delegate to Update Screen in Windows Application using C#

Hi,

I am working on C# windows application.

I had progressbar and come lablel wich always updated the screen.

I had used below code to update progressbar on screen.

Now I want to update labels, which updates record counts.

Can someone help me how to do this.

If I do not use delegate than my screen never updates, but the process always goes on.

I am submitting my code below which updates progressbar very nicely.

But if I add label which has count increment, my screen got white till the process completes.

If I touch the screen.

Can anyone help me in this regard

Thanks in Advance
0
dinesh_bali
Asked:
dinesh_bali
  • 3
  • 3
  • 2
1 Solution
 
dinesh_baliAuthor Commented:
Here is my code which works for progressbar

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ProgressBar
{
    public partial class Form1 : Form
    {
        int MaxCount = 0;
       
        public Form1()
        {
            InitializeComponent();
           
        }
        public delegate void ProgressHandler();
       
        private void button1_Click(object sender, EventArgs e)
        {
            MaxCount = int.Parse(txtCount.Text.ToString());
            progressBar1.Minimum = 0;
            progressBar1.Maximum = MaxCount;
            progressBar1.Value = 1;
            progressBar1.Step = 1;

            MethodInvoker mi = new MethodInvoker(StartLoop);
            mi.BeginInvoke(null, null);
        }
       
        private void UpdateProgressBar()
        {
            progressBar1.PerformStep();
           // progressBar1.Refresh();
        }
       
        private void StartLoop()
        {
            StringBuilder sb = new StringBuilder();
            for (int l = 0; l < MaxCount; l++)
            {
                //sb.Append(l);
                ProgressHandler phandler = new ProgressHandler(UpdateProgressBar);
                progressBar1.Invoke(phandler, null);
                Console.WriteLine(l);
            }
        }
    }
}
0
 
AlexFMCommented:
Why do you use MethodInvoker - do you have some specific reason to do this?
MSDN docimentation doesn't provide any useful information about this class. Possibly you need Thread.Start instead of this to run StartLoop in a worker thread.
StartLoop function looks OK assuming that it runs in a worker thread.
0
 
dinesh_baliAuthor Commented:
Hi,

Can you please correct the above code with thread

Basically, i want 3 int parameters to be passed in the UpdateProgressBar() method.

Which will update the screen Regularly.

I hope u help me

Many thanks

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mohamed ZedanCommented:
All you have to do is insert this line in the progress update loop this will allow everything to show correctly but becareful that when you do this your form will be resposive and buttons can be clicked while your work is done in the loop so be sure to disable the controls that you want non interactive

System.Windows.Forms.Application.DoEvents();
0
 
dinesh_baliAuthor Commented:

Hi,

i had already used Application.DoEvents();

but I want my controlls to be active and perform the task as soon as user click the button.

So, I cannot use Application.DoEvents();
 method.

Can you help me more.
0
 
Mohamed ZedanCommented:
DoEvents won't affect the reponse of the user click It will operate as you want ... what i am warning you about is that other buttons will be active too ... so if your task take long and you click on the close button for example the form will close ...but in order for the label to respond to the text changes it needs the DoEvents...
0
 
Mohamed ZedanCommented:
specially when in fast loops...
0
 
AlexFMCommented:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ProgressBar
{
    public partial class Form1 : Form
    {
        int MaxCount = 0;
       
        public Form1()
        {
            InitializeComponent();
           
        }
        public delegate void ProgressHandler();
       
        private void button1_Click(object sender, EventArgs e)
        {
            MaxCount = int.Parse(txtCount.Text.ToString());
            progressBar1.Minimum = 0;
            progressBar1.Maximum = MaxCount;
            progressBar1.Value = 1;
            progressBar1.Step = 1;

            Thread newThread = new Thread(new ThreadStart(StartLoop));          // << changed
            newThread.Start();                                                                           // << changed
        }
       
        private void UpdateProgressBar()
        {
            progressBar1.PerformStep();
           // progressBar1.Refresh();
        }
       
        private void StartLoop()
        {
            StringBuilder sb = new StringBuilder();
            for (int l = 0; l < MaxCount; l++)
            {
                //sb.Append(l);
                ProgressHandler phandler = new ProgressHandler(UpdateProgressBar);
                progressBar1.Invoke(phandler, null);
                Console.WriteLine(l);
            }
        }
    }
}

I think that StartLoop  is only prototype of real function. In its current state it does nothing except very fast loop. To test it better you can add line
Thread.Sleep(...);
to the loop to see progressbar change. When loop does some real work which takes time, remove Sleep.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now