M
Marco Segurini
Hi all,
I have a simple form with a textbox and a button.
The button is used to start/stop an acquisition thread that update the
text of the textbox. Since the textbox has been create in a thread
different from the acquisition thread I use InvokeRequired/Invoke to
update the textbox.
When I click the button to stop the acquisition thread I have the
problem to notify the acquisition thread to finish and then I want the
main thread wait until the other thread finish.
A solution is:
this._thread.Abort(); // abort the acquisition thread
this._thread.Join(); // wait until acquisition thread finish
but I dislike it.
Another solution is:
set a 'public' variable (visible to acquisition thread procedure)
this._thread.Join(); // wait until acquisition thread finish
but this deadlocks the program.
Thanks for any help.
Marco
/// 'Complete' sample problem ///
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 DeadLock
{
public partial class Form1 : Form
{
private System.Windows.Forms.Button _buttonStartStop;
private System.Windows.Forms.TextBox _textBox;
System.Threading.Thread _thread = null;
bool _bStopAcquisition;
public Form1()
{
InitializeComponent();
}
void MyWorkProc()
{
for (int i = 0; !this._bStopAcquisition; i = ++i % 1000)
{
SetEditBoxText(i.ToString());
}
}
delegate void MyDelegate(string str);
void SetEditBoxText(string str)
{
if (this._textBox.InvokeRequired)
{
this._textBox.Invoke(new MyDelegate(SetEditBoxText), new
object[] { str });
}
else
{
this._textBox.Text = str;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (this._thread == null)
{
this._bStopAcquisition = false;
this._thread = new System.Threading.Thread(MyWorkProc);
this._thread.Start();
this._buttonStartStop.Text = "Stop";
}
else
{
//this._thread.Abort(); // I don't want to use this statement
this._bStopAcquisition = true;
this._thread.Join();
this._buttonStartStop.Text = "Start";
this._thread = null;
}
}
}
}
I have a simple form with a textbox and a button.
The button is used to start/stop an acquisition thread that update the
text of the textbox. Since the textbox has been create in a thread
different from the acquisition thread I use InvokeRequired/Invoke to
update the textbox.
When I click the button to stop the acquisition thread I have the
problem to notify the acquisition thread to finish and then I want the
main thread wait until the other thread finish.
A solution is:
this._thread.Abort(); // abort the acquisition thread
this._thread.Join(); // wait until acquisition thread finish
but I dislike it.
Another solution is:
set a 'public' variable (visible to acquisition thread procedure)
this._thread.Join(); // wait until acquisition thread finish
but this deadlocks the program.
Thanks for any help.
Marco
/// 'Complete' sample problem ///
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 DeadLock
{
public partial class Form1 : Form
{
private System.Windows.Forms.Button _buttonStartStop;
private System.Windows.Forms.TextBox _textBox;
System.Threading.Thread _thread = null;
bool _bStopAcquisition;
public Form1()
{
InitializeComponent();
}
void MyWorkProc()
{
for (int i = 0; !this._bStopAcquisition; i = ++i % 1000)
{
SetEditBoxText(i.ToString());
}
}
delegate void MyDelegate(string str);
void SetEditBoxText(string str)
{
if (this._textBox.InvokeRequired)
{
this._textBox.Invoke(new MyDelegate(SetEditBoxText), new
object[] { str });
}
else
{
this._textBox.Text = str;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (this._thread == null)
{
this._bStopAcquisition = false;
this._thread = new System.Threading.Thread(MyWorkProc);
this._thread.Start();
this._buttonStartStop.Text = "Stop";
}
else
{
//this._thread.Abort(); // I don't want to use this statement
this._bStopAcquisition = true;
this._thread.Join();
this._buttonStartStop.Text = "Start";
this._thread = null;
}
}
}
}