Proxy for UI

  • Thread starter Thread starter Tod Johnson
  • Start date Start date
T

Tod Johnson

Hello,

as I know UI should be updated only from the main thread. That's why
I've created ProxyClass [1]. The question - is it normal solution or
maybe there are other, the best suggestions for this problem?

Thnx,
Tod


[1]
public class UIUpdater
{
Control _destinationControl;
object [] _args;
UIUpdate _uiDelegate;
EventHandler _uiDelegateEH;

public UIUpdater(Control c)
{
_destinationControl = c;
}

public static EventHandler CreateProxyEventHandler(Control c,
EventHandler e)
{
return (new UIUpdater(c)).GetProxyEventHandler(e);
}

private EventHandler GetProxyEventHandler(EventHandler e)
{
this._uiDelegateEH = e;
return new EventHandler(InvokerEH);
}

void InvokerEH(object sender, EventArgs e)
{
_uiDelegateEH(this, new EventArgs());
}
}

usage on Form:
ct = new ConnectionThreadCE(r);
ct.Connected += UIUpdater.CreateProxyEventHandler(this, new
EventHandler(ct_Connected));
 
Tod Johnson said:
as I know UI should be updated only from the main thread. That's why
I've created ProxyClass [1]. The question - is it normal solution or
maybe there are other, the best suggestions for this problem?

Well, I can't see anything in the code you've posted which actually
proxies the call to the UI thread. You've got _destinationControl, but
you haven't used it anywhere...
 
Ouch, that's true... When I was trying to make it smaller I cut off
usefull pieces :)

Of course the method InvokerEH must be in this form:

<code>
void InvokerEH(object sender, EventArgs e)
{
_destinationControl.Invoke(_uiDelegateEH);
}
</code>

and complete list:

<code>
public class UIUpdater
{
Control _destinationControl;
object [] _args;
UIUpdate _uiDelegate;
EventHandler _uiDelegateEH;

public UIUpdater(Control c)
{
_destinationControl = c;
}

public static EventHandler CreateProxyEventHandler(Control c,
EventHandler e)
{
return (new UIUpdater(c)).GetProxyEventHandler(e);
}

private EventHandler GetProxyEventHandler(EventHandler e)
{
this._uiDelegateEH = e;
return new EventHandler(InvokerEH);
}

void InvokerEH(object sender, EventArgs e)
{
_destinationControl.Invoke(_uiDelegateEH);
}
}

usage on Form:
ct = new ConnectionThreadCE(r);
ct.Connected += UIUpdater.CreateProxyEventHandler(this, new
EventHandler(ct_Connected));

Tod Johnson said:
as I know UI should be updated only from the main thread. That's why
I've created ProxyClass [1]. The question - is it normal solution or
maybe there are other, the best suggestions for this problem?


Well, I can't see anything in the code you've posted which actually
proxies the call to the UI thread. You've got _destinationControl, but
you haven't used it anywhere...
 
Tod Johnson said:
Ouch, that's true... When I was trying to make it smaller I cut off
usefull pieces :)

Of course the method InvokerEH must be in this form:

<code>
void InvokerEH(object sender, EventArgs e)
{
_destinationControl.Invoke(_uiDelegateEH);
}
</code>

That's more like it.

I haven't used such a thing myself - usually I write thread-safe
methods in the form, and add handlers which call those methods. Your
way certainly keeps things neater though. You might want to consider
using InvokeRequired to see whether to call Invoke or not.
 
Back
Top