GKR why the labels are not visible (C# code)

Permabanned
Joined
15 Nov 2006
Posts
164
Location
Visit Athens
This is a part of CPU Stretcher's source code:

Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.IO;
namespace CPU_Stretcher
{
	public class frmCPU_Stretcher : System.Windows.Forms.Form
	{
        	private System.ComponentModel.IContainer components=null;
		private System.Windows.Forms.Button btn_fire;
	        private System.Windows.Forms.Button btn_stop;
        	private System.Windows.Forms.PictureBox pictureBox_fire;
		private Label label_time;
	        private Label label_percentage;
		public frmCPU_Stretcher()
		{
			InitializeComponent();
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.Run(new frmCPU_Stretcher());
		}
		private void InitializeComponent()
        	{
	            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmCPU_Stretcher));
        	    this.btn_fire = new System.Windows.Forms.Button();
	            this.btn_stop = new System.Windows.Forms.Button();
        	    this.pictureBox_fire = new System.Windows.Forms.PictureBox();
	            this.label_time = new System.Windows.Forms.Label();
	            this.label_percentage = new System.Windows.Forms.Label();
        	    this.SuspendLayout();
		    // 
        	    // btn_fire
  	            // 
        	    this.btn_fire.FlatStyle = System.Windows.Forms.FlatStyle.System;
	            this.btn_fire.Font = new System.Drawing.Font("Times New Roman", 9F);
	            this.btn_fire.Location = new System.Drawing.Point(120, 248);
        	    this.btn_fire.Name = "btn_fire";
	            this.btn_fire.Size = new System.Drawing.Size(60, 24);
        	    this.btn_fire.TabIndex = 78;
	            this.btn_fire.Text = "Fire";
	            this.btn_fire.Click += new System.EventHandler(this.btn_fire_Click);
        	    // 
	            // btn_stop
        	    // 
	            this.btn_stop.Enabled = false;
        	    this.btn_stop.FlatStyle = System.Windows.Forms.FlatStyle.System;
	            this.btn_stop.Font = new System.Drawing.Font("Times New Roman", 9F);
        	    this.btn_stop.Location = new System.Drawing.Point(240, 218);
	            this.btn_stop.Name = "btn_stop";
        	    this.btn_stop.Size = new System.Drawing.Size(60, 24);
	            this.btn_stop.TabIndex = 79;
        	    this.btn_stop.Text = "Stop";
	            // 
        	    // pictureBox_fire
	            // 
        	    this.pictureBox_fire.BackColor = System.Drawing.Color.White;
	            this.pictureBox_fire.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox_fire.Image")));
        	    this.pictureBox_fire.Location = new System.Drawing.Point(186, 108);
	            this.pictureBox_fire.Name = "pictureBox_fire";
        	    this.pictureBox_fire.Size = new System.Drawing.Size(105, 96);
	            this.pictureBox_fire.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
        	    this.pictureBox_fire.TabIndex = 74;
	            this.pictureBox_fire.TabStop = false;
        	    this.pictureBox_fire.Visible = false;
	            // 
        	    // label_percentage
	            // 
        	    this.label_percentage.AutoSize = true;
	            this.label_percentage.BackColor = System.Drawing.Color.Black;
        	    this.label_percentage.Font = new System.Drawing.Font("Book Antiqua", 12.75F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | 			System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(161)));
	            this.label_percentage.ForeColor = System.Drawing.Color.OrangeRed;
        	    this.label_percentage.Location = new System.Drawing.Point(212, 80);
	            this.label_percentage.Name = "label_percentage";
	            this.label_percentage.Size = new System.Drawing.Size(70, 24);
        	    this.label_percentage.TabIndex = 94;
	            this.label_percentage.Text = "000.00%";
        	    this.label_percentage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
	            this.label_percentage.Visible = false;
	            // 
        	    // label_time
	            // 
        	    this.label_time.AutoSize = true;
	            this.label_time.BackColor = System.Drawing.Color.Black;
        	    this.label_time.Font = new System.Drawing.Font("Book Antiqua", 12.75F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | 				System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(161)));
	            this.label_time.ForeColor = System.Drawing.Color.Lime;
        	    this.label_time.Location = new System.Drawing.Point(134, 80);
	            this.label_time.Name = "label_time";
        	    this.label_time.Size = new System.Drawing.Size(67, 24);
	            this.label_time.TabIndex = 95;
        	    this.label_time.Text = "00:00:00";
	            this.label_time.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        	    this.label_time.Visible = false;
	            // 
        	    // frmCPU_Stretcher
	            // 
        	    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
	            this.BackColor = System.Drawing.Color.White;
        	    this.ClientSize = new System.Drawing.Size(449, 300);
	            this.Controls.Add(this.label_percentage);
        	    this.Controls.Add(this.label_time);
	            this.Controls.Add(this.btn_stop);
        	    this.Controls.Add(this.btn_fire);
	            this.Controls.Add(this.pictureBox_fire);
        	    this.Cursor = System.Windows.Forms.Cursors.Arrow;
	            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
        	    this.Location = new System.Drawing.Point(270, 20);
	            this.MaximizeBox = false;
        	    this.MinimizeBox = false;
	            this.Name = "frmCPU_Stretcher";
        	    this.ShowInTaskbar = false;
	            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
        	    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
	            this.Text = "CPU stretcher";
        	    this.TopMost = true;
	            this.ResumeLayout(false);	
                }
		private void btn_fire_Click(object sender, System.EventArgs e)
		{
			Thread t1 = new Thread(new ThreadStart(burn_in));
			pictureBox_fire.Visible=true;
        		btn_stop.Enabled=true;
			btn_fire.Enabled=false;
			label_time.Visible = true;  // This ******* label is appeared during execution but if we move
	        	t1.Start();                 // the command in burn_in() routine is not appeared GKR, GKR, GKR
		}   
		private void burn_in()
		{
        	    label_percentage.Visible = true;       // WTF this command does not work?????
	            System.Threading.Thread.Sleep(3000);   // Assume this command is the burn-in code
	            btn_stop.Enabled = false;
        	    label_percentage.Visible = false;
	            label_time.Visible = false;            // This command works GKR......
        	    pictureBox_fire.Visible = false;
	            btn_fire.Enabled = true;
		}
	}
}

The program was written in Visual C# 2003.
Yesterday I compiled the source code with Visual Studio 2005.
All OK, no warnings.
Then running the program I encountered problems (that's why I added comments).
Why?????? :confused:
I am very confused........ :(
 
Controls should never be accessed outside the thread they were created on. This means that you'll need to add methods to update the interface on the main UI thread that can be called from the burn-in thread.

Something like this should do:
Code:
private void UpdateInterface()
{
    if (InvokeRequired)
    {
        Invoke(new MethodInvoker(UpdateInterface));
        return;
    }
    
    // Do your stuff here.
}

Basically this method first checks that it's on the right thread (i.e. the main UI thread), and if it's not, calls itself on the right one, then returns. If it is on the right thread, it jut updates the controls.

By the looks of it, you'll need two such methods: one to do the pre-burn-in interface update, and one for the post-burn-in.

Of course, you could just do the interface updating on the main thread to begin with; that'd be a lot simpler :p
 
Last edited:
Thank you :) I will follow your recommendations.
I was very angry and confused cos I was thinking "both labels are external (global) variables)" so what's wrong.... :rolleyes:
 
Back
Top Bottom