Solved

Label created in new thread wont display

Posted on 2008-10-06
4
253 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
ID: 22648684
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
ID: 22648702
Hi,

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

0
 

Author Comment

by:mhouldridge
ID: 22649183
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
ID: 22655575
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Close form "before" open 3 41
Web Form VB.Net  import CSV 4 39
Reading the Web.Config using IIS 7.5? 4 39
FTP file download using c# 3 23
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 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