Avatar of Member_2_4458446
Member_2_4458446

asked on 

Using Delegates to update a GUI control from a different thread.

I am trying to load a form with a button. This button will have countdown text on it, that when the time elapses will close the form. I am using a delegate to update the button's text from a seperate thread, as I have seen this method work in other examples. The problem is I don't think I am starting the thread at the correct place. I start the new thread in the form's constructor. I think this is the cause fo the problem, but I don't know where else to start it, I could not find a load complete event for the form.

Thanks in advance for the help.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
 
namespace tsp
{
    public partial class errorDialog : Form
    {
        delegate void delegateCountdownExitButton();
 
        int timeout;
 
        void updateExitButton()
        {
            this.buttonExit.Text = string.Format("Exit {0}", timeout.ToString());
        }
 
        public errorDialog(string message, int timeout)
        {
            this.timeout = timeout;
            InitializeComponent();
            this.labelErrorMessage.Text = message.Trim();
            this.buttonExit.Text = "Exit";
            this.buttonExit.Click += new EventHandler(buttonExit_Click);
            StartThread()
        }
 
        public void StartThread()
        {
            while (timeout > 0)
            {
                Thread t = new Thread(new ThreadStart(ThreadJob));
                t.IsBackground = true;
                t.Start();
            }
        }
 
        void ThreadJob()
        {
            delegateCountdownExitButton dlceb = new delegateCountdownExitButton(updateExitButton);
            dlceb();
            timeout--;
            Thread.Sleep(1000);
        }
 
        void buttonExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

Open in new window

.NET Programming

Avatar of undefined
Last Comment
Member_2_4458446
Avatar of philipjonathan
philipjonathan
Flag of New Zealand image

What about the Form's Load event?
Avatar of i_am_robin_hood
i_am_robin_hood

Try
        public errorDialog(string message, int timeout)
        {
            this.timeout = timeout;
            InitializeComponent();
            this.labelErrorMessage.Text = message.Trim();
            this.buttonExit.Text = "Exit";
            this.buttonExit.Click += new EventHandler(buttonExit_Click);
        }
        public errorDialog_Load(object sender, EventArgs e)
        {
            StartThread();
        }

Open in new window

                   //if not on UI thread
                    if (Control.InvokeRequired)
                        Control.Invoke(new WriteLineDelegate(WriteLine), new object[] {logInfo});
                    else
                    {
                        WriteLine(logInfo);
                    }

        void ThreadJob()
        {
                    //if not on UI thread
                    if (buttonExit.InvokeRequired)
                        buttonExit.Invoke(new delegateCountdownExitButton(updateExitButton));
                    else
                    {
                        updateExitButton();
                    }
            timeout--;
            Thread.Sleep(1000);
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of i_am_robin_hood
i_am_robin_hood

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of bhermer
bhermer

ping
Avatar of Member_2_4458446

ASKER

Solution did it.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo