0

It is possible that if you do not copy in me application for x minutes. That the program then closes to a few minutes? Can someone explain to me how I can handle that?

EDIT: I wanted my application to close when no body is copying for instance in 2 minutes.

How can i solve my problem?

This is my code.

        protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        {
            const int WM_DRAWCLIPBOARD = 0x308;
            if (m.Msg == WM_DRAWCLIPBOARD)
            {
                // Kopieert en kijkt of het overeen komt met de list
                var text = Clipboard.GetText(TextDataFormat.UnicodeText);
                // als je gekopieert hebt reset de clipboard
                if (!string.IsNullOrEmpty(text))
                {
                    timer1.Interval = 15000;
                    GetAnswer(Clipboard.GetText(TextDataFormat.UnicodeText));
                    Clipboard.Clear();
                }
            }
        }
    }


    private void Timer1_Tick(object sender, EventArgs e)
    {
        this.Close();
    }
}

}

  • 1
    You can use timer, which is reset every time you get message. Exit software in timer event. – Sinatr Aug 20 '19 at 08:36
  • @Sinatr Could you perhaps show me an example to help me get started? –  Aug 20 '19 at 08:37

1 Answers1

1

1st drop a Timer component on your mainform, and set its Interval to as many minutes you want (the value is in milliseconds, so make sure you calculate it right)

Now Set the Enabled property of the timer to true

In the Tick event all you need to write is code to exit your application.

Now, everytime a copy/paste operation occurs, reset the timer like this

Timer1.Interval = xxx;

where xxx is the value in milliseconds

Resetting the timer can be found here

That should do it

in your case it will look something like this

if (!string.IsNullOrEmpty(text))
{
    timer1.Enabled = false; // stop the timer

    // do code here that can take some time...
    GetAnswer(Clipboard.GetText(TextDataFormat.UnicodeText));
    Clipboard.Clear();

    timer1.Interval = 15000; // reset the timer
    timer1.Enabled = true;   // and start it again
}
GuidoG
  • 11,359
  • 6
  • 44
  • 79
  • so I want an if else statement in which if the user does not copy for 5 minutes, the program closes automatically how can I best write that? –  Aug 20 '19 at 09:01
  • not by an `if else` statement, but by the method in this answer – GuidoG Aug 20 '19 at 09:02
  • Can you give a example to help me get started? –  Aug 20 '19 at 09:03
  • its all there, just start with this and if your stuck then show us what you have and then we can help you – GuidoG Aug 20 '19 at 09:03
  • I did this code i did a this.close but how can i reset the timer if i copied something. Because now my program is closing everytime after 10 secs ```private void Timer1_Tick(object sender, EventArgs e) { this.Close(); }``` –  Aug 20 '19 at 09:13
  • 10 seconds is not much time, I would set the Interval higher. You can reset the timer by the code shown in the answer. Do these 2 lines of code just before your code where you handle the copying – GuidoG Aug 20 '19 at 09:15
  • @rrwe I have edited my answer, with another method to reset the timer. It seems that this method would be saver to reset a timer – GuidoG Aug 20 '19 at 09:19
  • I almost succeeded. See my code at the top of my question. The few errors that are still there are when I copy again. The timer always stops at 30 seconds, so the code does not execute more than 2x. And if I copy at 13 seconds, it will only stop at 30 seconds. I want since I have my interval at 15000 that it stops at 28 sec. –  Aug 20 '19 at 09:28
  • @rrwe how long does the code takes that you execute when something is copied ? You could disable the timer at the start of your code, and then reset it to 15000 and enable it again at the end of your code – GuidoG Aug 20 '19 at 09:31
  • @rrwe I updated my answer with a possible solution for this – GuidoG Aug 20 '19 at 09:34