Thread-Save WinForm GET calls

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;
}
}
 
G

Guest

In your GetText method when InvokRequired is true it does not return a
string as defined by the delegate. The invoke function needs to return a
value. See below.

private string GetText()
{
if (this.textBox1.InvokeRequired)
{
GetTextCallback d = new GetTextCallback(GetText);
object returnedCallBackObject= this.Invoke(d, new object[] {
});
return (string)returnedCallBackObject;
}
else
{
return this.textBox1.Text;
}
}
 
A

abc

In your GetText method when InvokRequired is true it does not return a
string as defined by the delegate. The invoke function needs to return a
value. See below.

private string GetText()
{
if (this.textBox1.InvokeRequired)
{
GetTextCallback d = new GetTextCallback(GetText);
object returnedCallBackObject= this.Invoke(d, new object[] {});

return (string)returnedCallBackObject;
}
else
{
return this.textBox1.Text;
}
}



abc said:
I have been following all the example about how to SET a form element
property in athread-safemanner (see SET example below), but I can
not find any examples about how toGETa form element property in a
thread-safemanner. I tried using the code in theGETsection 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 newthread
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 newthread
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;
}
}- Hide quoted text -

- Show quoted text -

that works well.

Thanks a million
 
A

abc

In your GetText method when InvokRequired is true it does not return a
string as defined by the delegate. The invoke function needs to return a
value. See below.

private string GetText()
{
if (this.textBox1.InvokeRequired)
{
GetTextCallback d = new GetTextCallback(GetText);
object returnedCallBackObject= this.Invoke(d, new object[] {});

return (string)returnedCallBackObject;
}
else
{
return this.textBox1.Text;
}
}



abc said:
I have been following all the example about how to SET a form element
property in athread-safemanner (see SET example below), but I can
not find any examples about how toGETa form element property in a
thread-safemanner. I tried using the code in theGETsection 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 newthread
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 newthread
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;
}
}- Hide quoted text -

- Show quoted text -

that works well.

Thanks a million
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top