using System;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private TextBox textBox1;
private Button btnStart;
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.btnStart = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(134, 27);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 23);
this.btnStart.TabIndex = 0;
this.btnStart.Text = "Start";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(12, 29);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 1;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.btnStart);
this.Name = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
/// <summary>
/// Updates the textbox
/// </summary>
/// <param name="i">Value to be updated</param>
private void UpdateTextBox(int i)
{
textBox1.Text = i.ToString();
}
/// <summary>
/// Start counting
/// </summary>
/// <param name="i">Intial value</param>
private void Count(int i)
{
while (true)
{
i++;
UpdateTextBox(i);
}
}
private void btnStart_Click(object sender, EventArgs e)
{
Count(0);
}
}
}
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private TextBox textBox1;
private System.ComponentModel.BackgroundWorker backgroundWorker1;
private Button btnStart;
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.btnStart = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
this.SuspendLayout();
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(134, 27);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 23);
this.btnStart.TabIndex = 0;
this.btnStart.Text = "Start";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(12, 29);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 1;
//
// backgroundWorker1
//
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
//
// Form1
//
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.btnStart);
this.Name = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
/// <summary>
/// Updates the textbox
/// </summary>
/// <param name="i">Value to be updated</param>
private void UpdateTextBox(int i)
{
textBox1.Text = i.ToString();
}
/// <summary>
/// Start counting
/// </summary>
/// <param name="i">Intial value</param>
private void Count(int i)
{
while (true)
{
i++;
this.Invoke(new Action<int>((x) => textBox1.Text = x.ToString()), i);
}
}
private void btnStart_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
Count(0);
}
}
}
The other was to achieve the same is using Delegate BeginInvoke method
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private TextBox textBox1;
private Button btnStart;
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.btnStart = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(134, 27);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 23);
this.btnStart.TabIndex = 0;
this.btnStart.Text = "Start";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(12, 29);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 1;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.btnStart);
this.Name = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
private void UpdateTextBox(int i)
{
textBox1.Text = i.ToString();
}
private void Count(int i)
{
while (true)
{
i++;
this.Invoke(new Action<int>((x) => textBox1.Text = x.ToString()), i);
}
}
private void btnStart_Click(object sender, EventArgs e)
{
int i = 0;
new Action<int>(Count).BeginInvoke(i, null, null);
}
}
}
using System;
using System.Timers;
using System.Threading;
using System.Windows.Forms;
using System.Threading.Tasks;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private TextBox textBox1;
private Button btnStart;
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.btnStart = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(134, 27);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 23);
this.btnStart.TabIndex = 0;
this.btnStart.Text = "Start";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(12, 29);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 1;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.btnStart);
this.Name = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
private void UpdateTextBox(int i)
{
textBox1.Text = i.ToString();
}
private void Count()
{
int i=0;
while (true)
{
this.Invoke(new Action<int>((x) => textBox1.Text = x.ToString()), i++);
}
}
private void btnStart_Click(object sender, EventArgs e)
{
Task.Factory.StartNew(new Action(Count));
}
}
}
using System;
using System.Timers;
using System.Threading;
using System.Windows.Forms;
using System.Threading.Tasks;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private TextBox textBox1;
private Button btnStart;
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.btnStart = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(134, 27);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 23);
this.btnStart.TabIndex = 0;
this.btnStart.Text = "Start";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(12, 29);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 1;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.btnStart);
this.Name = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
private void btnStart_Click(object sender, EventArgs e)
{
Task.Factory.StartNew((Action)delegate()
{
int i = 0;
while (true)
{
this.Invoke(new Action<int>((x) => textBox1.Text = x.ToString()), i++);
}
});
}
}
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)