-1

What is the alternative of MethodInvoker in wpf
In windows formI use this code

private void msg()
{
   if (this.InvokeRequired)
        this.Invoke(new MethodInvoker(msg));
   else
        textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + readData;

In WPF I am using Dispatcher.CheckAccess() instead of this.InvokeRequired but there is no Dispatcher.MethodInvoke() or Dispatcher.Inovke.MethodInvoke() in wpf
If someone convert mycode to WPF it will be great

Edit:

Unknown Characters
enter image description here

Uzair Ali
  • 510
  • 14
  • 32
  • See here http://stackoverflow.com/questions/1644079/how-can-i-use-the-dispatcher-invoke-in-wpf-change-controls-from-non-main-thread and here http://stackoverflow.com/questions/19009174/dispatcher-invoke-vs-begininvoke-confusion – dbc Dec 13 '14 at 19:32
  • Also here: http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.invoke%28v=vs.110%29.aspx – dbc Dec 13 '14 at 19:33
  • I saw all the answered question but I didn't find the correct answer for my case – Uzair Ali Dec 14 '14 at 07:34

2 Answers2

0

Then you may use this:

public static class TextBoxExtensions
{
    public static void CheckAppendText(this TextBoxBase textBox, string msg, bool waitUntilReturn = false)
    {
        Action append = () => textBox.AppendText(msg);
        if (textBox.CheckAccess())
        {
            append();
        }
        else if (waitUntilReturn)
        {
            textBox.Dispatcher.Invoke(append);
        }
        else
        {
            textBox.Dispatcher.BeginInvoke(append);
        }
    }
}

And call it like

    private void msg()
    {
        textBox1.CheckAppendText(Environment.NewLine + " >> " + readData);
    }

When updating the UI from a background thread, you should generally use BeginInvoke rather than Invoke to avoid the possibility of a hang waiting for the UI thread to process the request while that thread is in turn waiting for something from the background thread.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • Its working fine appending the textbox data but also appending lot of unknown character signs – Uzair Ali Dec 14 '14 at 16:48
  • Check the Picture above – Uzair Ali Dec 14 '14 at 17:28
  • @UzairAli - I cannot reproduce your problem. Perhaps the bad characters are coming from `readData`? If `readData` is actually binary then you could get that effect. You can see the contents of `readData` in Visual Studio or by calling `Debug.WriteLine(readData)`. – dbc Dec 14 '14 at 18:59
  • Nope I checked `readData` just before executing this method its totally fine I used this code `private void msg() { MessageBox.Show(readData); textBox1.CheckAppendText(Environment.NewLine + " >> " + readData); }` – Uzair Ali Dec 14 '14 at 19:28
  • 1
    Oh yes this is the problem my side. `MessageBox.Show(readData.Text)` is showing the text correctly but when I check the length of that text with this `MessageBox.Show(readData.Text.Length)` it is ridiculously long (10k words). – Uzair Ali Dec 14 '14 at 19:44
0

This is the basic corresponding code for MethodInvoker and UI thread in WPF:

if (control.Dispatcher.CheckAccess())
  UpdateUI();
else
  control.Dispatcher.Invoke(new Action(UpdateUI));
M.G.E
  • 371
  • 1
  • 10