0

I have created a wpf applicaton. Which has one check box and two buttons for start and stop timer.After clicking on start button System.Timers.Timer aTimer will start running and calls the method checkboxstatus() to get the check box status and show it to the user. Even though if checkbox is checked am getting message as False. I have used the following code

public partial class MainWindow : Window
{  
    System.Timers.Timer aTimer = new System.Timers.Timer();
    bool ischeckboxchecked = false;

    public MainWindow()
    {
        InitializeComponent();
        aTimer.Elapsed += new ElapsedEventHandler(senddata_Tick);
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        aTimer.Interval = 3000;
        aTimer.Start();
    }
    public string checkboxstatus()
    {
        string data = string.Empty;
        ischeckboxchecked = false;
        Dispatcher.BeginInvoke((Action)(() =>
        {
            if (checkBox1.IsChecked == true)
            {
                ischeckboxchecked = true; //value is updating on each timer tick
            }
        }));
        data += ischeckboxchecked.ToString();
        return data;
    }
    private void senddata_Tick(Object sender, EventArgs args)
    {
        string postdata = string.Empty;
        postdata = checkboxstatus(); //every time am getting data as false
        MessageBox.Show(postdata);
    }
    private void button2_Click(object sender, RoutedEventArgs e)
    {
        aTimer.Stop();
    }

    private void checkBox1_Checked(object sender, RoutedEventArgs e)
    {

    }
}

any one suggest .......

Daniel Kelley
  • 7,579
  • 6
  • 42
  • 50
user3274485
  • 109
  • 1
  • 8
  • Did you try to make `ischeckboxchecked` a string and directly put "true" or "false" inside? – Kilazur Jul 03 '14 at 14:25
  • Closed. See my answer on the linked question to understand how WPF works and how to properly use WPF UI features in a multi-threaded scenario. – Federico Berasategui Jul 03 '14 at 14:27
  • @HighCore not a duplicate; the subjects discussed in the other question do not relate to the issue presented in this question. If it should be marked as a duplicate then an extensive comment should accompany it explaining how data binding may or may not be a solution to the problem here. – Bas Jul 03 '14 at 14:29
  • @BasBrekelmans sorry, DataBinding is ALWAYS a solution to horrible non working code behind winforms-like stuff in WPF. The `real` solution here is to delete all that horrible code behind and use proper DataBinding. Hence I'm closing this to let the OP read my answer to the duplicate question and properly understand WPF. – Federico Berasategui Jul 03 '14 at 14:31
  • @HighCore I agree with your views regarding data binding, though maybe not as strongly. However I feel that any person has the right to choose if data binding is a solution for them and marking this as a duplicate is by any means incorrect because it does not address or explain the problem posed by the OP. – Bas Jul 03 '14 at 14:35

1 Answers1

1

You are invoking BeginInvoke on the dispatcher with your method. BeginInvoke immediately returns. Use Invoke instead to get a blocking call and return only after the Dispatcher operation has completed.

Bas
  • 26,772
  • 8
  • 53
  • 86