I am trying to test running a threadpool timer in a class outside of a form class and have the event from the timer create a message in a texBox in my main Form. Not sure teh best way to do this...I can't see the textBox in the other object...
I can easily instantiate the object and start teh timer in a method from that object....but not sure how to create the event, AND use it in the code runnig in the Form.
I am working up to learning how to run a time consuming process (IO of course) in a separate thread then pass buffers of data to the form....and I 'm trying to start simple...no luck so far on the simple part...
Thanks
StefanPosted Apr 23, 2009, 10:49 PM
Hope this helps!
John RogersPosted Apr 27, 2009, 8:16 PM
Hey...I tried putting the timer event in to the Form and accessing a callback method within...and STILL can no t get to the textBox1 control.
Obviously, I am missing something VERY elemetary at this point. Any ideas for teh newbiw are appreciated.
JPR
namespace
TimerForm{
public partial class Form1{
///{
if (disposing && (components != null)){
components.Dispose();
}
base.Dispose(disposing);}
#region
Windows Form Designer generated code ///{
this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(12, 25); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "Start"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(12, 221); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 0; this.button2.Text = "Quit"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(12, 54); this.textBox1.MinimumSize = new System.Drawing.Size(0, 50); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(207, 161); this.textBox1.TabIndex = 1; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(289, 266); this.Controls.Add(this.textBox1); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout();}
#endregion
private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; public System.Windows.Forms.TextBox textBox1;}
}
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Linq;using
System.Text;using
System.Windows.Forms;namespace
TimerForm{
public partial class Form1 : Form{
// field definitions public string aString; public Form1(){
InitializeComponent();
// TestClass newObject = new TestClass();}
private void Form1_Load(object sender, EventArgs e){
aString =
"Load Form\r\n";textBox1.Text = aString;
}
private void button1_Click(object sender, EventArgs e){
aString +=
"Start Application\r\n";textBox1.Text = aString;
}
private void button2_Click(object sender, EventArgs e){
aString +=
"Stop Application\r\n";textBox1.Text = aString;
this.Close();}
static void dTick(object data){
for (int i = 0; i < 1000; i++); Form1.textBox1.Text = data;}
static void Main(){
System.Threading.
Timer tmr = new System.Threading.Timer(dTick,"state 1...",3000,1000); Application.Run(new Form1());tmr.Dispose();
}
}
}
John RogersPosted Apr 24, 2009, 7:03 PM
Ok...I moved a simple thread pool based timer into my form code...
Added a button whose click event starts a timer which has an event defined as dTick.
Try to access a public textBox control...in dTick() and can not see the control.
I can see it in other event methods in the Form1 but not in the dTick
private
void buttonTimerTest_Click(object sender, EventArgs e){
System.Threading.
Timer tmr = new System.Threading.Timer(dTick, "tick...", 5000, 1000);textBoxSetUPTabStatusMessage.Text =
"started...";}
private void dTick(object data){
//MainForm. //writes "tick..." to the console...}
// private void SendRptsBtn_Click() //-------------------------------------------StefanPosted Apr 24, 2009, 3:08 PM
Here's the catch: If you create a new instance of your form, you will not be accessing the current form. Communicating with a form that is already open can sometimes be difficult.
A simple fix would be to just move the event into your form ,which I don't think you want, so you may want to look up what delegates are and how to use them.
Hope this helps!
John RogersPosted Apr 24, 2009, 1:32 PM
I did make the textBOX control public...whcih is why I'm confused that I can't see it to set the Text property when ready. I tried using the Form1. qualifier and it does not offer me access to any controls.
I may try to create a property in my form that I can set with a new text string.
Here is the code:
Form:
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Linq;using
System.Text;using
System.Windows.Forms;namespace
TimerForm{
public partial class Form1 : Form{
public string aString; public Form1(){
InitializeComponent();
//TestClass newObject = new TestClass();}
private void Form1_Load(object sender, EventArgs e){
aString =
"Load Form\r\n";textBox1.Text = aString;
}
private void button1_Click(object sender, EventArgs e){
aString +=
"Start Application\r\n";textBox1.Text = aString;
}
private void button2_Click(object sender, EventArgs e){
aString +=
"Stop Application\r\n";textBox1.Text = aString;
this.Close();}
static void Main(){
TestClass TTT = new TestClass();TTT.TimerStart();
Application.Run(new Form1());}
}
}
Class:
using
System;using
System.Collections.Generic;using
System.Linq;using
System.Text;using
System.Threading;namespace
TimerForm{
public class TestClass{
public void TimerStart(){
System.Threading.
Timer t = new System.Threading.Timer(dTick, "Tick...", 5000, 1000);}
public void dTick(object data){
for (int i = 0; i < 1000; i++) ; Form1.}
}
}
Thanks