marcfromm

marcfromm

  • NA
  • 35
  • 0

Trying to figure out Threading

Dec 8 2004 11:10 AM
In this small program I start a thread in button1_Click method. In buttonQuit_Click method I wanted to abort the thread, because when I do quit the program, the program keeps running in the background and I must terminate it in Windows task manager. I could not abort the thread in buttonQuit_Click because "thread1" (defined in button1_Click) is not known to buttonQuit_Click. I tried to define thread1 outside of any method (like my "int data1;") but I received syntax errors about not liking the method in new ThreadStart( messageDisplay1 ) namespace ThreadTest { . . . int data1; available to all mehtods public Form1() { . . . static void Main() { Application.Run(new Form1()); } private void buttonQuit_Click(object sender, System.EventArgs e) { Application.Exit(); } private void buttonClear_Click(object sender, System.EventArgs e) { for (int i = 0; i < this.Controls.Count; i++) if (this.Controls[i] is TextBox) this.Controls[i].Text = ""; } private void button1_Click(object sender, System.EventArgs e) { data1 = int.Parse(textBox1.Text); Thread thread1 = new Thread( new ThreadStart( messageDisplay1 ) ); thread1.Priority = ThreadPriority.Lowest; thread1.Start(); } private void messageDisplay1() { int temp; temp = data1; for (int i=0; i<=15000; i++) { temp = data1*i; textBoxMessages.Text += "Data1: " + data1.ToString() +"\r\n"; textBoxMessages.Text += "I: " + i.ToString()+"\r\n"; textBoxMessages.Text += "Data1 * I = " + temp.ToString()+"\r\n"; } return; } } }

Answers (2)