I am a relatively new c# user so forgive me if I have made some rookie mistakes. I have searched for a workaround to this error but am unable to find a implementation which works for me.
I have a worker thread which is basically looping while boolean isCalibrationActive
is true. During the worker thread I have set up a callback to access a label in the main thread and change the text based on the worker thread. On the formclosing event in the main thread i set isCalibrationActive
to false which should theoretically stop the loop in the worker thread. I get the error once i close the form.
Form Closing Event Code:
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
DialogResult result = MessageBox.Show("The Calibration stage is not yet complete. Are you sure you want to exit?", "Dialog Title", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
isCalibrationActive = false;
Thread.Sleep(10000);
return;
}
else
{
e.Cancel = true;
}
}
else
{
e.Cancel = true;
}
}
Worker Thread: EDIT: sorry i left my test code in there earlier.
private void refreshXY()
{
var currentpath = new StringBuilder(255);
//store current directory and set to dll directory.
UnsafeNativeMethods.GetDllDirectory(currentpath.Length, currentpath);
UnsafeNativeMethods.SetDllDirectory( "D:\\UI_still_in_progress\\Debug" );
UnsafeNativeMethods.LoadLibrary("dllTESTER.dll");
while (isCalibrationActive)
{
xx = UnsafeNativeMethods.grabx();
yy = UnsafeNativeMethods.graby();
if (xx == 0) { xx = 0.001;}
if (yy == 0) { yy = 0.001;}
SetXlab(xx.ToString());
SetYlab(yy.ToString());
}
//restore old directory
UnsafeNativeMethods.SetDllDirectory(currentpath.ToString());
}
The error occurs during the SetXlab
or SetYlab
functions which are basically identical. Also the reason i have it in another thread is because it is reading real time data from another application which needs to be consistently updated. This is shown below:
private void SetYlab(string yval)
{
// 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.yshow.InvokeRequired)
{
SetTextCallback yD = new SetTextCallback(SetYlab);
if (isCalibrationActive) { this.Invoke(yD, new object[] { yval }); }
}
else
{
this.yshow.Text = yval;
}
}
Specifically the line this.Invoke(yD, new object[] { yval }); }
is what causes the error. Any ideas how I can avoid this?