Avatar of Natan1978
Natan1978
Flag 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
C#

Avatar of undefined
Last Comment
Natan1978

8/22/2022 - Mon
Minh Võ Công

You can use  timer with WPF. here is a sample:
http://www.c-sharpcorner.com/uploadfile/mahesh/timer-in-wpf/
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
Minh Võ Công

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Natan1978

ASKER
yes but impletent the counter plz inside code

thanks
Minh Võ Công

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
Natan1978

ASKER
wher i insert this?
i want to show the time run reverce that use show this all the time
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Natan1978

ASKER
plz
i wait you

thanks
Minh Võ Công

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
Natan1978

ASKER
hi
i try this but have erros
plz look on files

thanks
Home2.xaml
Home2.xaml.cs
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Minh Võ Công

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;
                }
          }
          }
Natan1978

ASKER
Hi
What is counter= your counter
Ehat is this?
ASKER CERTIFIED SOLUTION
Minh Võ Công

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Natan1978

ASKER
Is ok o understand now
I will try this

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

Thanks
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Natan1978

ASKER
thank you