Update Windows Form UI thread from another class c#
delegate void UpdateLabelDelegate(string LabelText);
public partial class Form1 : Form
{
private Class1 _class;
UpdateLabelDelegate ULD;
Dispatcher uiDispatcher;
public void UpdateLabel(string LabelText)
{
lbl_ClientCount.Text = LabelText;
}
public void UpdateThread(string LabelText)
{
uiDispatcher.Invoke(ULD, new object[] { LabelText });
}
public Form1()
{
InitializeComponent();
_class = new Class1(this);
ULD = new UpdateLabelDelegate(UpdateLabel);
uiDispatcher = Dispatcher.CurrentDispatcher;
_class.UpdateUI()
}
}
class Class1
{
public static int ClientConnected = 0;
private Form1 _form;
public Class1(Form1 form)
{
//
this._form = form;
}
public void UpdateUI()
{
ClientConnected++;
_form.UpdateThread(Convert.ToString(ClientConnected));
}
}
public partial class Form1 : Form
{
private Class1 _class;
UpdateLabelDelegate ULD;
Dispatcher uiDispatcher;
public void UpdateLabel(string LabelText)
{
lbl_ClientCount.Text = LabelText;
}
public void UpdateThread(string LabelText)
{
uiDispatcher.Invoke(ULD, new object[] { LabelText });
}
public Form1()
{
InitializeComponent();
_class = new Class1(this);
ULD = new UpdateLabelDelegate(UpdateLabel);
uiDispatcher = Dispatcher.CurrentDispatcher;
_class.UpdateUI()
}
}
class Class1
{
public static int ClientConnected = 0;
private Form1 _form;
public Class1(Form1 form)
{
//
this._form = form;
}
public void UpdateUI()
{
ClientConnected++;
_form.UpdateThread(Convert.ToString(ClientConnected));
}
}
Comments
Post a Comment