【问题标题】:Exit application after x minutes of clipboard.gettext inactivity of Windows在clipboard.gettext 不活动x 分钟后退出应用程序
【发布时间】:2019-12-25 10:21:16
【问题描述】:

如果您在 x 分钟内没有复制我的应用程序,这是可能的。该程序然后关闭几分钟?有人可以向我解释我该如何处理吗?

编辑: 我希望我的应用程序在没有人复制时关闭,例如 2 分钟。

我该如何解决我的问题?

这是我的代码。

        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();
    }
}

}

【问题讨论】:

  • 您可以使用计时器,每次收到消息都会重置。在定时器事件中退出软件。
  • @Sinatr 你能给我举个例子来帮助我开始吗?

标签: c# winforms clipboard


【解决方案1】:

首先在主窗体上放置一个Timer 组件,并将其Interval 设置为您想要的分钟数(值以毫秒为单位,因此请确保您计算正确)

现在将定时器的 Enabled 属性设置为 true

Tick 事件中,您只需编写退出应用程序的代码。

现在,每次发生复制/粘贴操作时,像这样重置计时器

Timer1.Interval = xxx;

其中 xxx 是以毫秒为单位的值

重置定时器可以找到here

应该这样做

在你的情况下,它看起来像这样

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
}

【讨论】:

  • 所以我想要一个 if else 语句,如果用户在 5 分钟内没有复制,程序会自动关闭我怎样才能最好地写出来?
  • 不是通过if else 声明,而是通过此答案中的方法
  • 你能举个例子来帮助我入门吗?
  • 一切都在那里,从这里开始,如果你卡住了,那么告诉我们你有什么,然后我们可以帮助你
  • 我做了这个代码我做了一个this.close但是如果我复制了一些东西我怎么能重置计时器。因为现在我的程序每次 10 秒后都会关闭 private void Timer1_Tick(object sender, EventArgs e) { this.Close(); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
相关资源
最近更新 更多