且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

10分钟不活动应用程序关闭C#winform

更新时间:2023-02-12 08:03:03

这是怎么做的:

  .MouseMove + =(sender,eventArgs)= >  { //  实际参数t ypes由编译器根据已知事件类型推断 
// 从此处调用你想要的,使用eventArgs参数
};



即使你工作,你也可以使用匿名方法做同样的事情使用C#v.2尚未引入lambda表达式:

  this  .MouseMove + = deletage(  object  sender,MouseEventArgs eventArgs){
// ...
};









顺便说一下,永远不要使用像Form1_MouseMove这样的自动生成的名称 - 它们违反(好的)Microsoft命名约定;不要使用下划线,数字等。总是将所有这些名称重命名为语义。您认为重构引擎是什么?



-SA


请澄清:你希望在超时后关闭你的应用程序,或者它作为一个错误关闭,你想避免这种情况吗?在第一种情况下,您可以参考这篇文章:应用程序空闲 [ ^ ],在后一种情况下,您应该发布一些代码。

Hi,

My question is simple, I have created a program, but when there has been 10 minutes of inactivity, the application will Close, like this:

Application.Close();



I already search about Mouse Move event and there come some errors, in my Form1.Designer.cs is an error in this line:

this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);


The error says:

Cannot assign to 'MouseMove' because it is a 'method group'


Regards,
KZ

This is how to do it:
this.MouseMove += (sender, eventArgs) => { // actual parameter types are inferred by a compiler from the known event type
    // from here, call whatever you want, using eventArgs parameters
};


You can do the same using anonymous method even if you work with C# v.2 where lambda expression were not yet introduced:

this.MouseMove += deletage(object sender, MouseEventArgs eventArgs) { 
    // ...
};



[EDIT]

By the way, never ever use auto-generated names like "Form1_MouseMove" — they violate (good) Microsoft naming conventions; don''t use underscore, numerics, etc. Always rename all such names to something semantic. What do you think refactoring engine for?

—SA


Please clarify: you want to close you app after the timeout, or it is closing as a bug, and you want to avoid this? In the first case, you could consult this article: Application Idle[^], in the later case, you should post a little more of your code.