A C# Windows application? Presume you mean a legacy Windows Forms application then, i've taken the trouble to make an example explaining the concept. Basically you need to get your calculations away from the UI Thread, but remember to get the updates to your UI controls executed on it.
I've taken the liberty to use listboxes instead of textboxes, as textboxes are just silly to display running values in IMO, feel free to change that though if there is some compelling reason :)
So standard form with two listboxes in the leftmost part, a start and a stop button and a label indicating if the applications is running. I use a Random object to sleep the updates a different random period between 100-2000 milliseconds to imitate calculations.
using a timer is very common to ensure recurring events, when using such it has its own thread and care must be taken not to execute multiple occurances simultaneously (like when executing the occurance of the timed event takes longer than the time between event occurances.)
UPDATE: Now added and using numbers, as requester had that wish
Code behind:
using System;
using System.Threading;
using System.Windows.Forms;
namespace FormThings
{
public partial class TheForm : Form
{
private static Random Rnd { get; set; }
private delegate void ListboxUpdateDelegate(ListBox listbox);
public TheForm()
{
InitializeComponent();
Rnd = new Random((int)DateTime.Now.Ticks);
}
private volatile int _countOne = 0;
private volatile int _countTwo = 0;
private void TimeX_Tick(object sender, EventArgs e)
{
if (_timeExecuting)
return;
try
{
_timeExecuting = true;
int seedOne = Rnd.Next(100, 2000);
int seedTwo = Rnd.Next(100, 2000);
var state1 = new V35CompatibleContainer
{
ListBox = FirstListBox,
Seed = seedOne,
};
ThreadPool.QueueUserWorkItem(ExcecuteV35compatibleOnThreadPool, state1);
var state2 = new V35CompatibleContainer
{
ListBox = SecondListBox,
Seed = seedTwo,
};
ThreadPool.QueueUserWorkItem(ExcecuteV35compatibleOnThreadPool, state2);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception in time tick event: " + ex.ToString());
}
finally
{
_timeExecuting = false;
}
}
private volatile bool _timeExecuting = false;
private void ExcecuteV35compatibleOnThreadPool(object state)
{
var stateContainer = (V35CompatibleContainer)state;
UpdateControl(stateContainer.ListBox, stateContainer.Seed);
}
private class V35CompatibleContainer
{
public ListBox ListBox;
public int Seed;
}
private void UpdateControl(ListBox listBox, int seed)
{
System.Threading.Thread.Sleep(seed);
if (listBox.InvokeRequired)
{
listBox.BeginInvoke(new ListboxUpdateDelegate(InsertIncrementLoop), new object[] { listBox });
return;
}
InsertIncrementLoop(listBox);
}
private void InsertMoment(ListBox listBox)
{
listBox.Items.Insert(0, DateTime.Now.ToString("HH:mm:ss.fff"));
}
private void InsertIncrementLoop(ListBox listBox)
{
int theValueNumber = -1;
if (listBox.Name == "FirstListBox")
{
theValueNumber = Interlocked.Increment(ref _countOne);
if (theValueNumber == 10)
Interlocked.Exchange(ref _countOne, 0);
}
else
{
theValueNumber = Interlocked.Increment(ref _countTwo);
if (theValueNumber == 10)
Interlocked.Exchange(ref _countTwo, 0);
}
listBox.Items.Insert(0, theValueNumber.ToString());
}
private void ToggleRunmode()
{
StopButton.Enabled = !StopButton.Enabled;
StartButton.Enabled = !StopButton.Enabled;
if (StopButton.Enabled)
{
FirstListBox.Items.Clear();
SecondListBox.Items.Clear();
}
TimeX.Enabled = StopButton.Enabled;
RunningLabel.Visible = StopButton.Enabled;
}
private void StartButton_Click(object sender, EventArgs e)
{
ToggleRunmode();
}
private void StopButton_Click(object sender, EventArgs e)
{
ToggleRunmode();
}
private void TheForm_Load(object sender, EventArgs e)
{
}
}
}
Designer:
namespace FormThings
{
partial class TheForm
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.StartButton = new System.Windows.Forms.Button();
this.StopButton = new System.Windows.Forms.Button();
this.FirstListBox = new System.Windows.Forms.ListBox();
this.SecondListBox = new System.Windows.Forms.ListBox();
this.RunningLabel = new System.Windows.Forms.Label();
this.TimeX = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
this.StartButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.StartButton.Location = new System.Drawing.Point(337, 13);
this.StartButton.Name = "StartButton";
this.StartButton.Size = new System.Drawing.Size(75, 38);
this.StartButton.TabIndex = 0;
this.StartButton.Text = "&Start";
this.StartButton.UseVisualStyleBackColor = true;
this.StartButton.Click += new System.EventHandler(this.StartButton_Click);
this.StopButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.StopButton.Enabled = false;
this.StopButton.Location = new System.Drawing.Point(337, 57);
this.StopButton.Name = "StopButton";
this.StopButton.Size = new System.Drawing.Size(75, 38);
this.StopButton.TabIndex = 1;
this.StopButton.Text = "S&top";
this.StopButton.UseVisualStyleBackColor = true;
this.StopButton.Click += new System.EventHandler(this.StopButton_Click);
this.FirstListBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.FirstListBox.FormattingEnabled = true;
this.FirstListBox.Location = new System.Drawing.Point(13, 13);
this.FirstListBox.Name = "FirstListBox";
this.FirstListBox.Size = new System.Drawing.Size(122, 173);
this.FirstListBox.TabIndex = 2;
this.SecondListBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Right)));
this.SecondListBox.FormattingEnabled = true;
this.SecondListBox.Location = new System.Drawing.Point(141, 12);
this.SecondListBox.Name = "SecondListBox";
this.SecondListBox.Size = new System.Drawing.Size(122, 173);
this.SecondListBox.TabIndex = 3;
this.RunningLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.RunningLabel.AutoSize = true;
this.RunningLabel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))));
this.RunningLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.RunningLabel.ForeColor = System.Drawing.Color.White;
this.RunningLabel.Location = new System.Drawing.Point(320, 130);
this.RunningLabel.Name = "RunningLabel";
this.RunningLabel.Size = new System.Drawing.Size(92, 20);
this.RunningLabel.TabIndex = 4;
this.RunningLabel.Text = "RUNNING !";
this.RunningLabel.Visible = false;
this.TimeX.Interval = 250;
this.TimeX.Tick += new System.EventHandler(this.TimeX_Tick);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(424, 205);
this.Controls.Add(this.RunningLabel);
this.Controls.Add(this.SecondListBox);
this.Controls.Add(this.FirstListBox);
this.Controls.Add(this.StopButton);
this.Controls.Add(this.StartButton);
this.MinimumSize = new System.Drawing.Size(440, 244);
this.Name = "TheForm";
this.Text = "TheForm - A legacy windows application";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button StartButton;
private System.Windows.Forms.Button StopButton;
private System.Windows.Forms.ListBox FirstListBox;
private System.Windows.Forms.ListBox SecondListBox;
private System.Windows.Forms.Label RunningLabel;
private System.Windows.Forms.Timer TimeX;
}
}