0
   private void button1_Click(object sender, RoutedEventArgs e)
    {
        new Thread(delegate() { doTest(); }).Start();
    }

When doTest() included with in the mainwindow , as below , my textbox is updating from the thread , i can see text box value changing ( so updating value from thread is working .

   public void doTest()
        {
            int count = 20;
            while (count < 30)
            {
                 Dispatcher.Invoke(new Action(() => txtBoxLog.Text = count.ToString()), null);
                count++;
                Thread.Sleep(500);
            }
        }

But , if i remove the above code and make separate class , placing outside the main window class , updating my text box from the thread is failing and i m not getting any error but the simply not changing its value , i updating from thread using dispatcher not updating .

to access mainwindow from doTest() class i created internal static main winodwo calss tw

internal static MainWindow tw;

and now my doTest() method have following code in clsTest.cs file

internal class clsTest
{
    public clsTest() { }

    public void doTest()
    {
        int count = 20;
        while (count < 30)
        {
            MainWindow.tw.Dispatcher.Invoke(
             new Action(() => MainWindow.tw.txtLog.Text = count.ToString()), DispatcherPriority.Background, null);

            count++;
            Thread.Sleep(500);
        }
    }
}

please , assist me to identify where i am getting wrong here ..

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
panindra
  • 646
  • 2
  • 11
  • 33

3 Answers3

1

There is nothing obviously wrong with what you have posted. They look fine to me, and I double checked by running your snippets in a sample app. I would guess that it is something to do with how you are initialising the static "tw" MainWindow instance which you have not shown.

edit: Okay you should not be explicitly creating a new MainWindow instance like you are doing since WPF automatically creates one for you. Instead you should be setting that static instance to point at the default instance, e.g. in the constructor of your MainWindow class:

internal static MainWindow tw;

public MainWindow()
{
    InitializeComponent();
    tw = this;
}
donovan
  • 1,442
  • 9
  • 18
  • this is my declation of tw in the mainwindow - " internal static MainWindow tw = new MainWindow();" if i declared this as -"internal static MainWindow tw; - i am getting null exception at run time . – panindra Sep 10 '12 at 06:37
0

assist me to identify where i am getting wrong

It's the part where you use a dispatcher instead of just binding the Text.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • @panindra: What do you mean by "scope"? There is a dispatcher for every thread that needs one, and if you dispatch on the wrong dispatcher you get exceptions. – H.B. Sep 10 '12 at 01:42
  • i just asked ,is it not possible to use dispathcer method to call ui control( text box) from the thread when it is running in separate class outside the MainWindow thread. I am not getting nay error but my text box not updating values from the thread . no exceptions . – panindra Sep 10 '12 at 01:46
  • Of course it's possible, but i would not recommend it. Threads may just fail silently, are you sure it works? If not you can try stepping through it with a debugger, or catch possible exceptions and write them to the console (Ouput window). – H.B. Sep 10 '12 at 01:48
0

Don't you need to initialize the tw as tw = this instead of tw = new MainWindow()?

Bob
  • 381
  • 4
  • 14