10分钟不活动应用程序关闭C#winform [英] 10min Inactivity the application close C# winform

查看:90
本文介绍了10分钟不活动应用程序关闭C#winform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的问题很简单,我创建了一个程序,但是当有10分钟不活动时,应用程序将关闭,如下所示:

 Application.Close(); 





我已经搜索了鼠标移动事件并出现了一些错误,在我的Form1.Designer.cs中出现错误:

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



错误说:

无法分配给'MouseMove',因为它是'方法组'



问候,

KZ

解决方案

这是怎么做的:

  .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.


这篇关于10分钟不活动应用程序关闭C#winform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆