How to show text in textbox which is used in a method and it is called through thread timer. The real problem is shown on below code. Where text box value is not shown on text box of form while its value is shown on mesage box.
-
-------------------Program.cs-------------- using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace WindowsApplication1 { static class Program { [STAThread] static void Main(string[] args) { Application.SetCompatibleTextRenderingDefault(false); Form1 obj = new Form1(); TimerCallback tcallback = new TimerCallback(obj.Child); ThreadPool.QueueUserWorkItem(new WaitCallback(tcallback)); long dTime = 1000 ; long pTime = 10000 ; System.Threading.Timer atimer = new System.Threading.Timer(tcallback, null, dTime, pTime) ; Application.EnableVisualStyles(); Application.Run(new Form1()); } } } ------------------------------------------------------------------------- ----------------------------------------Form1.cs------------------------- using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void Child(object state) { //textBox1.Text is the problem. This value is not shown on form. textBox1.Text = "Some Text"; MessageBox.Show(textBox1.Text); } } } -------------------------------------------------
-
-------------------Program.cs-------------- using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace WindowsApplication1 { static class Program { [STAThread] static void Main(string[] args) { Application.SetCompatibleTextRenderingDefault(false); Form1 obj = new Form1(); TimerCallback tcallback = new TimerCallback(obj.Child); ThreadPool.QueueUserWorkItem(new WaitCallback(tcallback)); long dTime = 1000 ; long pTime = 10000 ; System.Threading.Timer atimer = new System.Threading.Timer(tcallback, null, dTime, pTime) ; Application.EnableVisualStyles(); Application.Run(new Form1()); } } } ------------------------------------------------------------------------- ----------------------------------------Form1.cs------------------------- using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void Child(object state) { //textBox1.Text is the problem. This value is not shown on form. textBox1.Text = "Some Text"; MessageBox.Show(textBox1.Text); } } } -------------------------------------------------
Hi.. Nice Subject :-D you can not change the text of a control from another thread. try the following:
public void Child(object state)
{
if (textBox1.InvokeRequired)
{
Invoke(new TimerCallback(Child),new object[]{state}); // Invoke the called method through Message-Loop
}
else
{
textBox1.Text = "Some Text"; // Interact with the Control
MessageBox.Show(textBox1.Text);
}
}alternatively you could also use System.Windows.Forms.Timer instead of the one in the Threading - Namespace. then you wouldn't have to use Invoke. hope this helps m@u