microsoft / RoomAliveToolkit

Other
715 stars 191 forks source link

UI thread update #12

Closed chijuiwu closed 9 years ago

chijuiwu commented 9 years ago

Hello,

Can you give you me some guidance on how to update WinForm controls on the projected screen? For example, I have a button in MainForm(pc screen), and after clicking that button, I want to change the text on a label in Form1(projection screen). It works if the button is in Form1, but not when it's in MainForm. I've tried calling invalidate, refresh, update, begininvoke, application.doevents, and background threads, but none of these fixes the problem. This is bizarre.

Thank you.

chijuiwu commented 9 years ago

I was being stupid. I should be doing work in ProjectorForm. For anyone who wants to update the UI on the projection screen via the pc screen (MainForm), see the following example:

In MainForm:

public event LabelTextChangedHandler LabelTextChanged;
public delegate void LabelTextChangedHandler(string text);
// your button click handler
private void button_Click(object sender, EventArgs e)
{
    this.LabelTextChanged("some_text");
}

In ProjectionMappingSample.cs:

foreach (ProjectorForm form in this.projectorForms)
{
    MainForm mainForm = userViewForm as MainForm;
    mainForm.LabelTextChanged += form.On_LabelTextChanged;
}

In ProjectorForm:

public void On_LabelTextChanged(string text)
{
    // the label you want to change
    this.label1.Text = text;
    this.label1.Invalidate();
}