By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,611 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,611 IT Pros & Developers. It's quick & easy.

Issue with threads, timer, and label

P: n/a
Hi, I'm having an issue with this code that I'm writing, I'm curious as
to why the windows form label (lblTime) isn't getting updated with the
new variable when I use the timer. I made a blank form with only the
timer and the label, and it works fine, is there something else I need
to do with the threads to get this to work? I'm a bit lost here.

Thanks in advance -- Aaryn
// Code Start
using System;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;

namespace MSKiller_GUI
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class MSKiller : System.Windows.Forms.Form
{
private static string[] globalArgs;
private Thread DoMyStuffThread;
private System.Windows.Forms.Timer timeLeftTimer;
private static int timeLeft;
private System.Windows.Forms.Label lblMessageOne;
private System.Windows.Forms.Label lblTime;
private System.Windows.Forms.Label lblProcess;
private System.Windows.Forms.ListBox lstBoxProcess;
private System.Windows.Forms.Button btnStop;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public MSKiller()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
InitializeThreads();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lblMessageOne = new System.Windows.Forms.Label();
this.lblTime = new System.Windows.Forms.Label();
this.btnStop = new System.Windows.Forms.Button();
this.lblProcess = new System.Windows.Forms.Label();
this.lstBoxProcess = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// lblMessageOne
//
this.lblMessageOne.AutoSize = true;
this.lblMessageOne.Location = new System.Drawing.Point(0, 0);
this.lblMessageOne.Name = "lblMessageOne";
this.lblMessageOne.Size = new System.Drawing.Size(109, 16);
this.lblMessageOne.TabIndex = 0;
this.lblMessageOne.Text = "Now shutting down...";
//
// lblTime
//
this.lblTime.AutoSize = true;
this.lblTime.Location = new System.Drawing.Point(8, 40);
this.lblTime.Name = "lblTime";
this.lblTime.Size = new System.Drawing.Size(84, 16);
this.lblTime.TabIndex = 1;
this.lblTime.Text = "In ## seconds...";
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(8, 64);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(88, 23);
this.btnStop.TabIndex = 2;
this.btnStop.Text = "Click to stop!";
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// lblProcess
//
this.lblProcess.AutoSize = true;
this.lblProcess.Location = new System.Drawing.Point(8, 16);
this.lblProcess.Name = "lblProcess";
this.lblProcess.Size = new System.Drawing.Size(73, 16);
this.lblProcess.TabIndex = 3;
this.lblProcess.Text = "<PROCESS>";
//
// lstBoxProcess
//
this.lstBoxProcess.Enabled = false;
this.lstBoxProcess.Location = new System.Drawing.Point(104, 0);
this.lstBoxProcess.Name = "lstBoxProcess";
this.lstBoxProcess.Size = new System.Drawing.Size(72, 95);
this.lstBoxProcess.TabIndex = 4;
//
// MSKiller
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(176, 96);
this.ControlBox = false;
this.Controls.Add(this.lstBoxProcess);
this.Controls.Add(this.lblProcess);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.lblTime);
this.Controls.Add(this.lblMessageOne);
this.Name = "MSKiller";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
globalArgs = args;
Application.Run(new MSKiller());
}

private void InitializeThreads()
{
DoMyStuffThread = new Thread(new ThreadStart(this.DoMyStuff));
DoMyStuffThread.Start();
}
private void DoMyStuff()
{
foreach (string s in globalArgs)
{
lstBoxProcess.Items.Add(s);
}

timeLeftTimer = new System.Windows.Forms.Timer();
timeLeftTimer.Interval = 1000;
timeLeftTimer.Enabled = true;
timeLeftTimer.Tick += new EventHandler(timeLeftTimer_Tick);
timeLeft = 30;
this.lblTime.Text = String.Format("In {0} seconds...", timeLeft);

foreach (string s in globalArgs)
{
lblProcess.Text = s;
Process[] AppToKill = Process.GetProcessesByName(s);
foreach (Process p in AppToKill)
{
timeLeft = 30;
if (p.CloseMainWindow() == false)
{
p.Kill();
}

else
{
timeLeftTimer.Start();
if (p.WaitForExit(30000) == false)
{
p.Kill();
}
timeLeftTimer.Stop();
}
}
}
DoMyStuffThread.Abort();
//Application.Exit();
}

private void btnStop_Click(object sender, System.EventArgs e)
{
DoMyStuffThread.Abort();
Application.Exit();
}

private void timeLeftTimer_Tick(object sender, System.EventArgs e)
{
timeLeft--;
if (timeLeft < 0) timeLeft = 0;
this.lblTime.Text = String.Format("In {0} seconds...", timeLeft);
}
}
}
// Code End
Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.