Solved

Using Delegate to Update Screen in Windows Application using C#

Posted on 2006-11-27
11
1,391 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short film showing how OnPage and Connectwise integration works.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

19 Experts available now in Live!

Get 1:1 Help Now