Link to home
Start Free TrialLog in
Avatar of Natan1978
Natan1978Flag for United States of America

asked on

counter in c# / wpf

hello
i want to creat counter that after user open application so it is start count his time he is use app and after xxx time messge box jump and ask him to approve that he is working on app and not go out, and other option is shutdown app.

how i do this?

thanks
Avatar of Cong Minh Vo
Cong Minh Vo
Flag of Viet Nam image

You can use  timer with WPF. here is a sample:
http://www.c-sharpcorner.com/uploadfile/mahesh/timer-in-wpf/
Avatar of Natan1978

ASKER

hello

i did this part but it is only show timer

i need that when application start so it will count xxx min's and when is end so it will ask user with messgebox if to contion work or to close app

this is what i done
 private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
           lblsleep.Content =(DateTime.Now.Hour.ToString() + ":" +
           DateTime.Now.Second.ToString());
        }

plz help me make it to what i need

thanks
You can get result of messagebox by dialogresult
DialogResult result = MessageBox.Show("Do you want close the app?", "Hi",
            MessageBoxButtons.OKCancel);
          switch (result)
          {
            case DialogResult.OK:
                {
                  this.Close();
                  break;
                }
            case DialogResult.Cancel:
                {
                  //your code here
                  break;
                }
          }
http://www.dotnetperls.com/messagebox-show
http://www.dotnetperls.com/dialogresult
yes but impletent the counter plz inside code

thanks
You set timer interval:
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000;
-> aTimer.Interval = Counter*1000*60; counter in minuter.
http://msdn.microsoft.com/en-us/library/system.timers.timer.interval.aspx
wher i insert this?
i want to show the time run reverce that use show this all the time
plz
i wait you

thanks
you use datetime substract function:

http://stackoverflow.com/questions/5177002/how-to-subtract-a-datetime-from-another-datetime.

You declare a variable DateTime
in form constract or onform_loading add a code line :
DateTime StartTime= DateTime.Now;

 private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
           lblsleep.Content =DateTime.Now.ToString("HH:mm:ss");
           int counter = DateTime.Now.SubStract(StartTime).Minutes ;
          if (counter == YourCounter)
          {
             DialogResult result = MessageBox.Show("Do you want close the app?", "Hi",
            MessageBoxButtons.OKCancel);
          switch (result)
          {
            case DialogResult.OK:
                {
                  this.Close();
                  break;
                }
            case DialogResult.Cancel:
                {
                  //your code here
                  break;
                }
          }
          }
        }
http://stackoverflow.com/questions/5177002/how-to-subtract-a-datetime-from-another-datetime
hi
i try this but have erros
plz look on files

thanks
Home2.xaml
Home2.xaml.cs
fix your code:
DateTime StartTime;
private void Window_Loaded(object sender, RoutedEventArgs e)
 {        
         DateTime StartTime= DateTime.Now;      
     }
   private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
              DateTime StartTime= DateTime.Now;
              lblsleep.Content =DateTime.Now.ToString("HH:mm:ss");
              int counter = DateTime.Now.SubStract(StartTime).Minutes ;

          if (counter == YourCounter)
          {
             DialogResult result = MessageBox.Show("Do you want close the app?", "Hi",
            MessageBoxButtons.OKCancel);
          switch (result)
          {
            case DialogResult.OK:
                {
                  this.Close();
                  break;
                }
            case DialogResult.Cancel:
                {
                  //your code here
                  break;
                }
          }
          }
Hi
What is counter= your counter
Ehat is this?
ASKER CERTIFIED SOLUTION
Avatar of Cong Minh Vo
Cong Minh Vo
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is ok o understand now
I will try this

Can you help with my quation about export wpf form to pdf?

Thanks
thank you