Click here to Skip to main content
15,917,565 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to quit the application if the user don't use it if he use it still open for hem

What I have tried:

try to make countdown timer bout it didn't work
it didn't work because it start count when the user login in bout that i want is start count if he didn't use the app and quit after 20 min

C#
private void Form1_Load(object sender, EventArgs e)
      {
          IdleTimer.Tick += new EventHandler(IdleTimer_Tick);
      }

      void Application_Idle(object sender, EventArgs e)
      {
          if (!IdleTimer.Enabled)
          {
              StartTimer();
          }
      }

      private void StartTimer()
      {
          IdleTimer.Interval = (Int32)((60000.0D) * (0.2D));
          IdleTimer.Enabled = true;
          Debug.WriteLine("Timer Started : " + IdleTimer.Interval.ToString());
          IdleTimer.Start();
      }

      void IdleTimer_Tick(object sender, EventArgs e)
      {
          IdleTimer.Enabled = false;
          // Some cleanup code
          // ....
          // ....
          Application.Exit(); // Or something like this
      }

      private void Form1_MouseMove(object sender, MouseEventArgs e)
      {
          RestartTimer();
      }

      private void Form1_KeyDown(object sender, KeyEventArgs e)
      {
          RestartTimer();
      }

      private void StopTimer()
      {
          Debug.WriteLine("Stopping Timer.");
          IdleTimer.Enabled = false;
          IdleTimer.Stop();
      }

      private void RestartTimer()
      {
          StopTimer();
          StartTimer();
      }


what is wrong with this code ??
Posted
Updated 21-Jan-19 2:07am
v3
Comments
CPallini 21-Jan-19 4:40am    
"try to make countdown timer bout it didn't work"
Please elaborate.
el_tot93 21-Jan-19 4:42am    
i have update it
PIEBALDconsult 21-Jan-19 11:43am    
Is that what the _user_ wants?
el_tot93 22-Jan-19 0:09am    
yes

Add a Timer instance to the app, set it to an Interval of 1000 (one second).
Handle the Tick event.
Add a class level DateTime, called closeAt, and write a private method called Tickle. Inside the method, set closeAt to DateTime.Now.AddMinutes(numberOfMinutesToIdleBeforeClosing)
Inside the Timer Tick handler method, compare closeAt to teh current DateTime. If it is greater, use Application.Exit[^] to close the app.
Now all you need to do is decide what "user don't use it" consists of, and call Tickle whenever he does something that constitutes "using" the app!
 
Share this answer
 
v2
Comments
el_tot93 21-Jan-19 4:48am    
can you explain it in a code if you have time
OriginalGriff 21-Jan-19 5:05am    
Explain which bit?
Remember, I don't know what you do or don't know - I only get exactly what you type to work with.

So: have you created a Timer instance?
el_tot93 21-Jan-19 5:30am    
look im new in coding so i don't know what should i do
OriginalGriff 21-Jan-19 5:49am    
So you don;t know how to create a Timer instance?
Why not? If you are working with stuff like this, you need to know the basics first. And we can't teach you those, that is what you are on your course for!
el_tot93 21-Jan-19 7:20am    
check now
I suppose others had already asked a similar question, see, for instance: Automatic Application Shutdown[^].
 
Share this answer
 
1- Add a Timer to your Form.

2-Add this method to your Form class.

C#
public static uint GetIdleTime()
{
     LASTINPUTINFO LastUserAction = new LASTINPUTINFO();
     LastUserAction.cbSize = 
(uint)System.Runtime.InteropServices.Marshal.SizeOf(LastUserAction);
     GetLastInputInfo(ref LastUserAction);
     return ((uint)Environment.TickCount - LastUserAction.dwTime);
}

3- in Form_Load start the timer:

C#
timer1.Start();

4- in timer tick event check GetIdleTime

C#
private void timer1_Tick(object sender, EventArgs e)
{
    if (GetIdleTime() > 1200000 )  // 20minutes
       Application.Exit();//For Example
}


<pre lang="c#">using System.Runtime.InteropServices;

[DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);   

internal struct LASTINPUTINFO
{
    public uint cbSize;

    public uint dwTime;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900