473,378 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Issue with threads, timer, and label

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
0 1519

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
14
by: Michael C | last post by:
Hi all, I'm now trying to update a status indicator via a Timer control on a worker thread. The status indicator is just a label on the main form, but I'm having trouble figuring out exactly...
6
by: RahimAsif | last post by:
Hi guys, I would like some advice on thread programming using C#. I am writing an application that communicates with a panel over ethernet, collects data and writes it to a file. The way the...
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
1
by: jmgro | last post by:
I have spent way too much time trying to solve the following problem: I have a datalist with a timer in the footer template. It works wonderfully except when the user pages back, then forward,...
8
by: Luc | last post by:
Hi, I am writing software to automate some testing. I have one main form to set up the tests, and once this is complete, I open 4 identical forms to monitor each different device that I am...
4
by: gsimmons | last post by:
I've been researching multi-threaded WinForms apps and thread synchronization stuff for a couple days since I'm working on refactoring a multi-threaded GUI app at work and want to be sure it's...
1
by: Siegfried Heintze | last post by:
The following code below is single threaded. When I click the start button, it hogs the message pump so none of the UI works. How can I convert this to make it multi-threaded so I can continue to...
9
by: brendan_gallagher_2001 | last post by:
Hi I am seeing some strange behaviour on a windows (vb.net 1.1) service. Basically, what I see happening is that when the Timer1_Elapsed event fires, it attempts to execute Timer1.Stop() but...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.