Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using Delegate to Update Screen in Windows Application using C#

Posted on 2006-11-27
11
Medium Priority
?
1,404 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
Independent Software Vendors: 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

636 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