-1

I am a bit new in programming C#. I would like to know if there is a way for my wpf to run as i want it to run.

I am creating a timer for a desktop application. What I would like it to do is that if it reached 5 minutes, the buttons for editing will be disabled and the buttons left are just for saving.

for example, When i open the microsoft word application on my desktop, i only have 5 minutes to access it, if i reached the time limit what i can only do is save it and other buttons will be disabled.

here is a clip of my code.

//-----------------------------------------------------------------------------------------------------
ticker.DispatcherTimerInstance.Tick += (ev, EV) =>
{
    TimeCount.Text = ticker.currentResult.ToString();
};

dp.AddValueChanged(TimeCount, (object a, EventArgs b) =>
{
    if (TimeCount.Text == "00:00:10")
    {
        dt_TimeStart_PW.Stop();
    }
    if (TimeCount.Text == "00:00:10")
    {
        win2.ShowDialog();




    }
    if (TimeCount.Text == "00:05:00")
    {

        win3.ShowDialog();
        //the buttons on the app should be disabled except for save and close

    }
});

dt_TimeStart_PW = new DispatcherTimer();
dt_TimeStart_PW.Start();
dt_TimeStart_TC = new DispatcherTimer();
dt_TimeStart_TC.Start();


dt_TimeStart_PW.Tick += (a, b) =>
{
    GetTaskNameWindows("Please wait...");
    //GetTaskNameWindows("Task Completed");
};
dt_TimeStart_TC.Tick += (a, b) =>
{

    GetTaskNameWindows("Task Completed");
};

Thank you. I hope you can help me.

S.R.
  • 1
  • 2
  • what language is this in? – WhatsThePoint May 30 '17 at 07:32
  • C#. sorry i forgot to input the language. – S.R. May 30 '17 at 08:04
  • Please don't use `TimeCount.Text == "00:05:00"` to determine when you should done a task as there is no guarantee that the timer will fire at the right time to set the text to that. Windows is not a real-time operating system. You need to set a time-stamp and compare against that. `var then = DateTime.Now;` and then test `then.AddMinutes(5.0) < DateTime.Now`. – Enigmativity May 31 '17 at 02:44

2 Answers2

0

Simply use this Code buttonName.IsEnabled=False; to disable the button when it reached 5 minute. Here Button Name is the Name of button and you can disable the buttons as you like when conditions are met

rohit poudel
  • 43
  • 1
  • 8
  • the buttons that i want to disable are the buttons on microsoft word application. can it be done? – S.R. May 31 '17 at 03:10
  • Please go through this: [link] (https://stackoverflow.com/questions/13074554/disable-the-save-as-button-in-word-2010) – rohit poudel May 31 '17 at 04:04
0

You should look at using Microsoft's Reactive Framework (NuGet "System.Reactive") to help with this.

Then you can do this:

var subscription =
    Observable
        .Timer(TimeSpan.FromMinutes(5.0))
        .ObserveOnDispatcher()
        .Subscribe(_ => button.IsEnabled = false);

That's the entire code you need.

If you want to stop the timer before the 5 minutes is up just do subscription.Dispose();.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172