简体   繁体   中英

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?

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM