Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I convert wpf dispatcher to winforms

I was moving over a method to my winforms project from a wpf project.

Everything but this section was moved without issue:

private void ServerProcErrorDataReceived(object sender, DataReceivedEventArgs e)
{
  // You have to do this through the Dispatcher because this method is called by a different Thread
  Dispatcher.Invoke(new Action(() =>
  {
    richTextBox_Console.Text += e.Data + Environment.NewLine;
    richTextBox_Console.SelectionStart = richTextBox_Console.Text.Length;
    richTextBox_Console.ScrollToCaret();
    ParseServerInput(e.Data);
  }));
}

I have no idea how to convert over Dispatcher to winforms.

Can anyone help me out?

like image 728
ErocM Avatar asked Aug 08 '13 19:08

ErocM


Video Answer


1 Answers

You should use Invoke to replace the Dispatcher.

private void ServerProcErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    if (richTextBox_Console.InvokeRequired)
    {
        richTextBox_Console.Invoke((MethodInvoker)delegate
        {
            ServerProcErrorDataReceived(sender, e);
        });
    }
    else
    {
        richTextBox_Console.Text += e.Data + Environment.NewLine;
        richTextBox_Console.SelectionStart = richTextBox_Console.Text.Length;
        richTextBox_Console.ScrollToCaret();
        ParseServerInput(e.Data);
    }
}
like image 162
Shimrod Avatar answered Sep 28 '22 08:09

Shimrod