0

enter image description here

If you open adobe photo shop, one small window is suddenly open. In that you can saw the running text Initilizing... Reading Fonts.. like that.

I like to do this type of running text in my project also..

I try to in for loop. but it not show!.

for (int j = 1; j <= 3; j++)
            {
                label1.Text = "Please Wait.";
                label1.Text = "Please Wait..";
                label1.Text = "Please Wait...";
                label1.Text = "Please Wait.";
                label1.Text = "Please Wait..";
                label1.Text = "Please Wait...";
                label1.Text = "Please Wait.";
                label1.Text = "Please Wait..";
                label1.Text = "Please Wait...";
                label1.Text = "Please Wait.";
                label1.Text = "Please Wait..";
                label1.Text = "Please Wait...";
                label1.Text = "Please Wait.";
                label1.Text = "Please Wait..";
                label1.Text = "Please Wait...";
            }

pls give a suggestion.,

Sagotharan
  • 2,586
  • 16
  • 73
  • 117
  • 1
    There are multiple duplicates floating around with the answer to this: [Force GUI update from UI Thread](http://stackoverflow.com/questions/1360944/winforms-force-gui-update-from-ui-thread) [Label text not updated](http://stackoverflow.com/questions/1921612/label-text-not-updated) [GUI not updating until code is finished](http://stackoverflow.com/questions/3959185/gui-not-updating-until-code-is-finished) and many, many more. **tl;dr** Stop blocking the UI-Thread. – Bobby Jun 03 '11 at 11:07
  • You're changing the text too quickly. Try throwing a Thread.Sleep(100); Application.DoEvents(); between each place you're trying to set the text. – Bob G Jun 03 '11 at 11:08
  • for (int j = 1; j <= 3; j++) { label1.Text = "Please Wait."; Thread.Sleep(100); label1.Text = "Please Wait.."; } it wont help me!. – Sagotharan Jun 03 '11 at 11:15
  • why you all comment here?. what is problem to post these things in answer part?. Is any special reason?. bzs it have only little space!. – Sagotharan Jun 03 '11 at 11:27

2 Answers2

1

you need to add intervals in between, otherwise you won't see the text except the last one. You'll also need to update the GUI as in Barfieldmv's comments below, so something like this:

For(int i =0; i<3;i++)
{
  label1.Text = "Please Wait.";
  label1.Update();
  system.Threading.Thread.Sleep(500);
  label1.Text = "Please Wait..";
  label1.Update();
  system.Threading.Thread.Sleep(500);
  label1.Text = "Please Wait...";
}
Bolu
  • 8,696
  • 4
  • 38
  • 70
  • 1
    You need 'non' blocking intervals between them. Otherwise you'll just see the last text since the last UI update. Adding a timer to 'update' text should give the UI time to refresh the screen. – CodingBarfield Jun 03 '11 at 11:12
  • Its work in button click event. but i want it in window load event. Is it possible?!. – Sagotharan Jun 03 '11 at 11:24
  • put the code in Form_Shown then. It will work but what you really want is a decent Splash Screen, see:http://www.codeproject.com/KB/cs/prettygoodsplashscreen.aspx – Bolu Jun 03 '11 at 11:27
0

The problem is that you're blocking the UI thread. The application is Single-Threaded, that means that whatever you perform will keep the application from redrawing the UI elements.

If you have excessive tasks to do, you should always perform them on a separate thread to ensure that the UI stays responsive. This can be established with a BackGroundWorker f.e.. It will perform the tasks on another thread and will keep sending updates to the UI thread without blocking it or slowing down the real work.

Shamelessly copied from the MSDN page and modified by me:

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace BackgroundWorkerSimple
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
        }

        private void startAsyncButton_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy != true)
            {
                // Start the asynchronous operation.
                backgroundWorker1.RunWorkerAsync();
            }
        }

        private void cancelAsyncButton_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.WorkerSupportsCancellation == true)
            {
                // Cancel the asynchronous operation.
                backgroundWorker1.CancelAsync();
            }
        }

        // This event handler is where the time-consuming work is done.
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            // Excessive comuptation goes here, you can report back via this:
            worker.ReportProgress(progressInPercent, additionalProgressAsObject);
        }

        // This event handler updates the progress.
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            resultLabel.Text = (e.ProgressPercentage.ToString() + "%");
        }

        // This event handler deals with the results of the background operation.
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true)
            {
                resultLabel.Text = "Canceled!";
            }
            else if (e.Error != null)
            {
                resultLabel.Text = "Error: " + e.Error.Message;
            }
            else
            {
                resultLabel.Text = "Done!";
            }
        }
    }
}
Bobby
  • 11,419
  • 5
  • 44
  • 69