Solved

Using Delegate to Update Screen in Windows Application using C#

Posted on 2006-11-27
11
1,390 Views
Last Modified: 2008-01-09
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
Comment
Question by:dinesh_bali
  • 3
  • 3
  • 2
11 Comments
 

Author Comment

by:dinesh_bali
ID: 18020003
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 18021781
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
 

Author Comment

by:dinesh_bali
ID: 18022661
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
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 18022846
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:dinesh_bali
ID: 18023309

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
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 18023353
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
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 18023355
specially when in fast loops...
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 18025579
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now