Acronis
Acronis

Reputation: 55

C# Advanced form "please wait"

My form is going to run some code that might take a while to execute. I would like to display a "please wait" message while the operation is running on the background.

I'd like to have that message in a form, one that I can control its visibility, and also its text, from other forms.

I'd also like it to be set to start in the Program.cs file.

My code, so far:

namespace KAN
{
    public partial class prosze_czekac : Form
    {
        public prosze_czekac()
        {
            InitializeComponent();
        }

        private delegate void OffVisible();
        public void Wylacz()
        {
            if (this.InvokeRequired)
                this.Invoke(new OffVisible(Wylacz));
            else
                this.Visible = false;
        }

        delegate void SetTextCallback(string text);
        public void ZmienTekst(string text)
        {
            if (this.InvokeRequired)
            {
                //SetTextCallback d = new SetTextCallback(this.ZmienTekst);
                Invoke(new SetTextCallback(this.ZmienTekst), text);
                //Invoke(d, new object[] { text });
            }
            else
            {
                this.Visible = true;
                this.Text = text;
                this.lblKomunikat.Text = text;
                this.Update();
            }
        }
    }
}

I do not know how to run a form, how to create an instance and as editing text. All this in any form, any thread. Is the above code is correct and how to use it to make it properly?

How am I so ready form "please wait" I would like to turn it on now in the initial class (Program.cs). Use it in any form design. Sample code, do not know if correct:

namespace KAN
{
    static class Program
    {
        public static prosze_czekac PleaseWait;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>        
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Thread thread = new Thread(new ThreadStart(PleaseWait.Show());

            PleaseWait.ZmienTekst("Please wait... Running the program");

            // long operation

            PleaseWait.Wylacz();

            Application.Run(new main());
        }

    }
}

namespace KAN
{
    public partial class main: Form
    {
        public main()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            // examples of long task in another form
            for (int i = 0; i < 5; i++)
            {
                Program.PleaseWait.ZmienTekst((i + 1).ToString());
                System.Threading.Thread.Sleep(1000);
            }

            Program.PleaseWait.Wylacz();
        }
    }
}

The first time I ask a question, please bear with me.

PS "Wylacz" is "exit" (void) and is meant to "hide" so that every time you do not initiate the form. "prosze_czekac" is "please wait".

Upvotes: 2

Views: 2272

Answers (1)

Kai Hartmann
Kai Hartmann

Reputation: 3154

Use the BackgroundWorker. The following code assumes, you have a button 'button1' in your form, which executes the worker, which starts the long running task on a different thread:

BackgroundWorker _worker;

// button click starts the execution of the lung running task on another thread
private void button1_Click(object sender, EventArgs e)
{
    label1.Visible = true; // show the label "please wait"
    _worker.RunWorkerAsync();
}

private void Form1_Load(object sender, EventArgs e)
{
    // initialize worker
    _worker = new BackgroundWorker();
    _worker.DoWork += new DoWorkEventHandler(worker_DoWork);
    _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_worker_RunWorkerCompleted);
}

// executes when long running task has finished
void _worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // hide the label
    label1.Visible = false;
}

// is called by 'RunWorkerAsync' and executes the long running task on a different thread
void worker_DoWork(object sender, DoWorkEventArgs e)
{
    // long running task (just an example)
    for (int i = 0; i < 1000000000; i++)
    {
    }
}

Upvotes: 2

Related Questions