Solved

Label created in new thread wont display

Posted on 2008-10-06
4
244 Views
Last Modified: 2012-05-05
Experts,

I have the following loop which is run in a new thread when the user clicks on start.  The label does not show, and I dont know why this is, although my messagebox does,  please help!
public static void RunLoop(int stop)

        {

            while (true)

            {

                if (stop == 1)

                {

                    break;

                }
 

                ArrayList serverlist = new ArrayList();

                serverlist = MySQLcontrol.GetServers();
 

                int x = 300;

                int y = 100;

                int count = 0;

                foreach (ServerObj so in serverlist)

                {

                    switch (so.ServerType)

                    {

                        case "Virus Scanner":

                            MessageBox.Show("HERE");

                            System.Windows.Forms.Label label = new System.Windows.Forms.Label();

                            label.Name = "label" + count.ToString();

                            label.Text = so.ServerName;

                            label.Location = new System.Drawing.Point(x, y);

                            label.Visible = true;

                            x = x + 50;

                            count++;

                            break;

                    }

                }

                Thread.Sleep(10000);

            }

        }

Open in new window

0
Comment
Question by:mhouldridge
  • 2
4 Comments
 
LVL 8

Expert Comment

by:rambovn
Comment Utility
System.Windows.Forms.Label label = new System.Windows.Forms.Label();
                            label.Name = "label" + count.ToString();
                            label.Text = so.ServerName;
                            label.Location = new System.Drawing.Point(x, y);
                            label.Visible = true;
this.Controls.Add(label);
or yourform.Controls.Add(label);

0
 

Author Comment

by:mhouldridge
Comment Utility
Hi,

thanks for the info, but the intellisense  does not contain a reference for Contols, only ControlCollection.

0
 

Author Comment

by:mhouldridge
Comment Utility
This is because I've added this to a new thread, and this new thread cant access the main thread which the form runs on.

0
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 500 total points
Comment Utility
threading in windows forms is a little bit peculiar and all the UI operations needs to be performed in the main therad and not in the child thread
please read the following article and it will make a few more things clearer
http://www.yoda.arachsys.com/csharp/threads/winforms.shtml
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

12 Experts available now in Live!

Get 1:1 Help Now