lyar1031
January 3rd, 2006, 02:36 PM
I have a single threaded applicaation and I am getting a error CANNOT ACCESS A DISPOSED OBJECT when I exit the application (Hitting the X on the top right hand corner of the Dialog Box).
Here is where the error is occurring! I will Show the code snipet
private void ThreadFunc()
{/* Thread Func */
/* Get current Thread Running */
Thread Current = Thread.CurrentThread;
while (!ThreadStop)
{
if (Current.IsAlive == true)
{
SharedLocation.GetData = SharedLocation.GetData + 1;
DisplayData(SharedLocation.GetData);
SetSelectListBox(SharedLocation.GetData);
Thread.Sleep(Constants.GetSleepTime);
}
}
}
By setting a Flag the thread will exit, which I do in OnFormClosing(Not the correct event name in C#) and I have a Stop Thread Button where I set the flag to stop the thread.
When I stop the thread using my button, then exit the application I get no error. However, when I just exit the Thread w/o pressing the button is where I get the error. Here is the code snipet for the application exiting and the stop thread button being pressed. They both do the same thing.
private void StopThread_Click(object sender, EventArgs e)
{//Button Stop Thread
ThreadStop = true;
SerialPort.Close();
}
private void SerialComm_FormClosed(object sender, FormClosedEventArgs e)
{//Form Closed
ThreadStop = true;
SerialPort.Close();
}
The error occurs in these functions in the thread fucntion at these lines
1. this.Invoke(d, new object[] { Index });
2. this.Invoke(d, new object[] { Data });
in methods
1. DisplayData(SharedLocation.GetData);
2. SetSelectListBox(SharedLocation.GetData)
Here is the code for these 2 functions!
private void SetSelectListBox(int Index)
{/* Select List Box Item From Thread */
// Thread Safe calls to Windows Controls!!
if (this.OutputListBox.InvokeRequired)
{
SetSelectCallback d = new SetSelectCallback(SetSelectListBox);
this.Invoke(d, new object[] { Index });
}
else
{
this.OutputListBox.SelectedItem = Index;
}
}
private void DisplayData(int Data)
{/* Display Data in List Box From Thread */
// Thread Safe calls to Windows Controls!!
// If the calling thread is different from the thread that
// created the control, this method creates a
// SetDataCallback and calls itself asynchronously using the
// Invoke method.
//
// If the calling thread is the same as the thread that created
// the control, then the Control/Set property is set directly.
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.OutputListBox.InvokeRequired)
{
SetDataCallback d = new SetDataCallback(DisplayData);
this.Invoke(d, new object[] { Data });
}
else
{
this.OutputListBox.Items.Add(Data);
}
}
Thanks. The reason for the 2 functions is that I was modifying a control from within my thread and I had read a post that I had to do it this way.
Thanks your help on this is greatly appreciated since I am stuck on this one!!
Here is where the error is occurring! I will Show the code snipet
private void ThreadFunc()
{/* Thread Func */
/* Get current Thread Running */
Thread Current = Thread.CurrentThread;
while (!ThreadStop)
{
if (Current.IsAlive == true)
{
SharedLocation.GetData = SharedLocation.GetData + 1;
DisplayData(SharedLocation.GetData);
SetSelectListBox(SharedLocation.GetData);
Thread.Sleep(Constants.GetSleepTime);
}
}
}
By setting a Flag the thread will exit, which I do in OnFormClosing(Not the correct event name in C#) and I have a Stop Thread Button where I set the flag to stop the thread.
When I stop the thread using my button, then exit the application I get no error. However, when I just exit the Thread w/o pressing the button is where I get the error. Here is the code snipet for the application exiting and the stop thread button being pressed. They both do the same thing.
private void StopThread_Click(object sender, EventArgs e)
{//Button Stop Thread
ThreadStop = true;
SerialPort.Close();
}
private void SerialComm_FormClosed(object sender, FormClosedEventArgs e)
{//Form Closed
ThreadStop = true;
SerialPort.Close();
}
The error occurs in these functions in the thread fucntion at these lines
1. this.Invoke(d, new object[] { Index });
2. this.Invoke(d, new object[] { Data });
in methods
1. DisplayData(SharedLocation.GetData);
2. SetSelectListBox(SharedLocation.GetData)
Here is the code for these 2 functions!
private void SetSelectListBox(int Index)
{/* Select List Box Item From Thread */
// Thread Safe calls to Windows Controls!!
if (this.OutputListBox.InvokeRequired)
{
SetSelectCallback d = new SetSelectCallback(SetSelectListBox);
this.Invoke(d, new object[] { Index });
}
else
{
this.OutputListBox.SelectedItem = Index;
}
}
private void DisplayData(int Data)
{/* Display Data in List Box From Thread */
// Thread Safe calls to Windows Controls!!
// If the calling thread is different from the thread that
// created the control, this method creates a
// SetDataCallback and calls itself asynchronously using the
// Invoke method.
//
// If the calling thread is the same as the thread that created
// the control, then the Control/Set property is set directly.
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.OutputListBox.InvokeRequired)
{
SetDataCallback d = new SetDataCallback(DisplayData);
this.Invoke(d, new object[] { Data });
}
else
{
this.OutputListBox.Items.Add(Data);
}
}
Thanks. The reason for the 2 functions is that I was modifying a control from within my thread and I had read a post that I had to do it this way.
Thanks your help on this is greatly appreciated since I am stuck on this one!!