Re: Thread-Save WinForm GET calls



On May 28, 10:41 pm, burlo <b...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
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" wrote:
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

.



Relevant Pages

  • Re: Reflection in 1.1 and 2.0 - generic, fast copy delegate?
    ... Your delegate solution is is still significantly more efficient that reflection. ... Also you need permissions to emit code with the LCG version whereas using delegates does not appear to require permissions beyond those for public reflection and so you can have a fast solution even in low trust environments. ... private string _firstName; ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Reflection in 1.1 and 2.0 - generic, fast copy delegate?
    ... I had a scenario where I was constructing lots of objects, which are loaded dynamically, and Activator.CreateInstance is notoriously slow, so I created a delegate from a DynamicMethod to call the constructor directly. ... But once more, why use LCG? ... private string _firstName; ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Thread-Save WinForm GET calls
    ... string as defined by the delegate. ... private string GetText() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Generic Delegate Types explained (example)
    ... Unless you have specifically stated that the generic type has to be a value type, it can always be a reference type. ... To change the value of a string you have to replace it with a new string instance, which you can not do unless you send the string by reference to the method. ... example of generic delegate types taken from p. 108 of the C# 3.0 ... static Util UtilStringChanger2AndMoreStaticVersion ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: EJB3 Newbie & Persistence
    ... private String name; ... CustomerCMP customer; ... public void setAddress{ ...
    (comp.lang.java.beans)