Re: Updating controls on a form
Vince P <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <001701c76e95$d5a807d0$80f81770$@net> |
In your DLL, can you make a member to store the Control object that is using
it?
public class DllClass
{
private System.Windows.Forms.Control control;
public DllClass(Control control)
{
this.control = control;
}
private void CallFromOtherThread()
{
if(control.InvokeRequired)
{
control.BeginInvoke(new
MethodInvoker(this.CallFromOtherThread), null);
}
else
{
// your code here
}
}
}
-
THESE are the times that try men's souls. The summer soldier and the
sunshine patriot will, in this crisis, shrink from the service of their
country; but he that stands by it now, deserves the love and thanks of man
and woman. Tyranny, like hell, is not easily conquered; yet we have this
consolation with us, that the harder the conflict, the more glorious the
triumph.
> -----Original Message-----
> From: Discussion forum for developers using Windows Forms to build apps
> and controls [mailto:[email protected]] On Behalf Of
> Kelly Baker
> Sent: Saturday, March 24, 2007 21:37
> To: [email protected]
> Subject: [DOTNET-WINFORMS] Updating controls on a form
>
> Unless my app is EXTREMELY simple, I get the cross-threading error
> message
> regularly when updating controls on a Windows form.
>
> My latest example involves a dll that runs a System.Threading.Timer,
> updating private shared variables. When it finishes updating, it
> raises a
> NewData event. My app responds to that event by updating display
> controls
> (labels in this case). I have to use a delegate and update them like
> this:
>
> Private Sub SetText(ByVal [text] as String)
> If Me.Messages.InvokeRequired Then
> Dim d as New SetTextCallBack(AddressOf SetText)
> Me.Invoke(d, New Object() {[text]})
> Else
> Me.Messages.Text = [text]
> End If
> End Sub
>
> I have to write a different routine like this for each control on the
> form
> that I want to update. Is there a better way? Please?