开发者

real time loop counter ouput wpf

What can I do if I want to have a text-box representing in real time the value of a loop counter in wpf?

appending working solution:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private delegate void UpdateTextBox(DependencyProperty dp, Object value);
...
private void MyMethod()
{
    ...
    int iMax=...;
    ...
    MyClass iMyClass = new MyClass(arguments);
        this.DataContext = iMyClass;
        UpdateTextBox updateTBox = new UpdateTextBox(textBlock1.SetValue);
        for (int i = 1; i <= iMax; i++)
        {
            iMyClass.MyClassMethod(i);
            Dispatcher.Invoke(updateTBox, System.Windows.Threading.DispatcherPriority.Background, new object[] { MyClass.MyPropertyProperty, iMyClass.myProperty });

        }

Here is the code I tried according to your suggestion, but it doesnt work, I get "0" written in the textbox, so I suppose the binding is OK, but the loop doesnt work. I also made the loop to write into another textbox directly by textbox2.text="a" inside the loop, but it didnt work either.

/// <summary>
/// Interaction logic for Window1.xaml
/// DOESNT WORK PROPERLY
/// </summary>
public partial class Window1 : Window
{


    public Window1()
    {
        InitializeComponent();
        TestClass tTest = new TestClass();
        this.DataContext = tTest ;
        tTest.StartLoop();
    }
}
public class TestClass : DependencyObject
{
    public TestClass()
    {
        bwLoop = new BackgroundWorker();

        bwLoop.DoWork += (sender, args) =>
        {

            // do your loop here -- this happens in a separate thread
            for (int i = 0; i < 10000; i++)
            {
                LoopCounter=i;
            }
        };
    }
    BackgroundWorker bwLoop;

    public int LoopCounter
    {
        get { return (int)GetValue(LoopCounterProperty); }
        set { SetValue(LoopCounterPr开发者_StackOverflow中文版operty, value); }
    }

    public static readonly DependencyProperty LoopCounterProperty = DependencyProperty.Register("LoopCounter", typeof(int), typeof(TestClass));

    public void StartLoop()
    {
        bwLoop.RunWorkerAsync();
    }
}

}


  1. Run the loop in a background process (so that the UI can update itself while the loop is running) and

  2. write the loop counter into a property (DependencyProperty or CLR property with INotifyPropertyChanged) which is bound to a TextBox in your user interface. Alternatively, you can directly change the value of the TextBox via Dispatcher.Invoke (this is less elegant, though).

Does this help? Feel free to ask for clarification...


Code example (untested), using a DependencyProperty (which must be bound to a TextBox):

BackgroundWorker bwLoop;
public static readonly DependencyProperty LoopCounterProperty = 
    DependencyProperty.Register("LoopCounter", typeof(int),
    typeof(Window1), new FrameworkPropertyMetadata(0));

public int LoopCounter  {
    get { return (int)this.GetValue(LoopCounterProperty); }
    set { this.SetValue(LoopCounterProperty, value); } 
}

private MyWindow() {
    ...
    bwLoop = new BackgroundWorker();

    bwLoop.DoWork += (sender, args) => {
        ...
        for (int i = 0; i < someLimit; i++) {
            Dispatcher.Invoke(new Action(() => LoopCounter=i));
            System.Threading.Thread.Sleep(250); // do your work here
        }
    }

    bwLoop.RunWorkerCompleted += (sender, args) => {
        if (args.Error != null)
            MessageBox.Show(args.Error.ToString());
    };
}

private void StartLoop() {
    bwLoop.RunWorkerAsync();
}


you can use Data Binding for continuously updating the text.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜