?
Solved

Using Delegate to Update Screen in Windows Application using C#

Posted on 2006-11-27
11
Medium Priority
?
1,401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 
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
 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

762 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