A
abc
I have been following all the example about how to SET a form element
property in a thread-safe manner (see SET example below), but I can
not find any examples about how to GET a form element property in a
thread-safe manner. I tried using the code in the GET section below,
but I got the error message "not all code paths return a value". I
don't really know what I am doing. Please help.
######### SET #########
delegate void SetTextCallback(string text);
private void ThreadProcSafe()
{
// this function has been called on a new thread
this.SetText("This text was set safely.");
}
private void SetText(string text)
{
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}
######### GET #########
delegate string GetTextCallback();
private void ThreadProcSafe()
{
// this function has been called on a new thread
string s = this.GetText();
}
private string GetText()
{
if (this.textBox1.InvokeRequired)
{
GetTextCallback d = new GetTextCallback(GetText);
this.Invoke(d, new object[] { });
}
else
{
return this.textBox1.Text;
}
}
property in a thread-safe manner (see SET example below), but I can
not find any examples about how to GET a form element property in a
thread-safe manner. I tried using the code in the GET section below,
but I got the error message "not all code paths return a value". I
don't really know what I am doing. Please help.
######### SET #########
delegate void SetTextCallback(string text);
private void ThreadProcSafe()
{
// this function has been called on a new thread
this.SetText("This text was set safely.");
}
private void SetText(string text)
{
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}
######### GET #########
delegate string GetTextCallback();
private void ThreadProcSafe()
{
// this function has been called on a new thread
string s = this.GetText();
}
private string GetText()
{
if (this.textBox1.InvokeRequired)
{
GetTextCallback d = new GetTextCallback(GetText);
this.Invoke(d, new object[] { });
}
else
{
return this.textBox1.Text;
}
}