I have two WinForms applications and I need to add text to TextBox in Application1 from Application2. I've successfully done this using named pipes and WCF. I can successfuly call a method in Application1 from Application2 but I'm getting either "Invoke or BeginInvoke cannot be called on a control until the window handle has been created.” error or the textbox is not updated at all.
Here's my basic code. GetMessage
is called by Application2. This one doesn't update TextBox at all:
public void GetMessage(string msg)
{
UpdateTextbox(msg);
}
private void UpdateTextbox(string msg)
{
this.textBox1.Text += msg + Environment.NewLine;
}
This one throws Invoke error:
public void GetMessage(string msg)
{
Action a = () => UpdateTextbox(msg);
textBox1.BeginInvoke(a);
}
I tried to cheat my way by forcing creation of the handle with this, but it doesn't update TextBox either:
public void GetMessage(string msg)
{
IntPtr handle = textBox1.Handle;
Action a = () => UpdateTextbox(msg);
textBox1.BeginInvoke(a);
}
What should I do?