-1

I want to display information on screen for the user general view about process!

In the beginning, I used another thread, but this started an exception. Now I don't know. How can I change value on the screen simultaneously with another process?

How can I use GetElapsedTime to Show millisecond elapsed during the process?

WPF (XAML Code)

<StackPanel>
    <Button Content="Start" Height="20" Width="50" HorizontalAlignment="Left" Name="ButtonStart" Click="ButtonStart_Click"/>
    <Label Content="Elapsed Time (Milliseconds):"/>
    <Label Name="LabelElapsedTime" Content="0"/>
</StackPanel>


public partial class MainWindow : Window
{
    private StatusOfWork _StatusOfWork;
    public MainWindow()
    {            
        InitializeComponent();
    }


    private void ButtonStart_Click(object sender, RoutedEventArgs e)
    {
        _StatusOfWork = new StatusOfWork();
        _StatusOfWork.CountN(10);
        this.Close();
    }
}

class StatusOfWork
{
    public DateTime StartDateTime { get; set; }

    public void CountN(int nTimes)
    {
        StartDateTime = DateTime.Now;
        for (int i = 1; i <= nTimes; i++)
        {
            //doing anything
            //Another Process
            //...
            Thread.Sleep(1000);
        }
    }

    public double GetElapsedTime()
    {
        TimeSpan timeSpan = DateTime.Now - StartDateTime;
        return timeSpan.TotalMilliseconds;
    }
}
i.do.stuff
  • 207
  • 3
  • 11
  • It's unclear what exactly you are trying to do here. Is there actually some background thread? Is it required? Why can't you use BackgroundWorker, or better `async/await`? – Clemens Dec 21 '18 at 17:47

1 Answers1

0

You need to use WPF data binding concepts. I suggest checking out this stackoverflow article which has several good links to various tutorials.

That being said, here's a change to your code that should get you started: I added a binding to your label LabelElapsedTime called LapsedTime.

<StackPanel>
    <Button Content="Start" Height="20" Width="50" HorizontalAlignment="Left" Name="ButtonStart" Click="ButtonStart_Click"/>
    <Label Content="Elapsed Time (Milliseconds):"/>
    <Label Name="LabelElapsedTime"
        Content="{Binding ElapsedTime, RelativeSource={RelativeSource AncestorType=Window}}"/>
</StackPanel>

and the binding maps to a dependency property of the same name LapsedTime on your main window like this:

public partial class MainWindow : Window
{
    private StatusOfWork _StatusOfWork;

    public MainWindow()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty ElapsedTimeProperty =
        DependencyProperty.Register(nameof(ElapsedTime), typeof(string), typeof(MainWindow));

    public string ElapsedTime
    {
        get { return (string)GetValue(ElapsedTimeProperty); }
        set { SetValue(ElapsedTimeProperty, value); }
    }

    private async void ButtonStart_Click(object sender, RoutedEventArgs e)
    {

        for(int count = 0; count < 10; count ++)
        {
            ElapsedTime = string.Format("{0}", count);
            await Task.Delay(1000);
        }
        this.Close();
    }
}

For simplicity sake, I kept the property a string but you may wish to use a different data type. You will need to use a ValueConverter. The links in the stackoverflow article mentioned above explain this in more details.

Additionally: I am guessing the exception you were getting with using a thread is probably a dispatch exception. This is a good article to help you understand how to fix that.

Clemens
  • 123,504
  • 12
  • 155
  • 268
i.do.stuff
  • 207
  • 3
  • 11
  • 1
    This will not work. The loop with Thread.Sleep will only block the UI thread. Make the Click handler async and replace Thread.Sleep with `await Task.Delay()`. Besides that `Binding LapsedTime` is missing a source. Either you set the DataContext of the Window to itself, or you use an ElementName or RelativeSource Binding. – Clemens Dec 21 '18 at 17:42
  • Given that the question didn't include complete source, I think the suggested answers is sufficient. To be honest, this question could have been flagged as not complete enough. It's pretty nice there's a answer that will help the question, period. – tatmanblue Dec 21 '18 at 17:47
  • @tatmanblue The answer is horrible. It doesn't explain anything, and the code won't work. Your upvote doesn't make it any better. Besides that the question should not (yet) be answered at all, because it's unclear what OP is actually trying to achieve. – Clemens Dec 21 '18 at 17:48
  • Also, `LapsedTimeDependency` is incomplete and wrong. The identifier field for a dependency property `LapsedTime` must be named `LapsedTimeProperty`, and it should be *shown* in the post. And you won't really use `string` as type for an "elapsed tIme". Use `TimeSpan` instead. – Clemens Dec 21 '18 at 17:52