热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在Windows应用程序中模拟会话-SimulatingsessioninaWindowsapp

Iamworkingonawindowsapplication.IneedtosimulateSession(thatwehaveinawebapp)inthe

I am working on a windows application. I need to simulate Session (that we have in a web app) in the win app where if the user is inactive for certain period, he gets logged off. The user login status is maintained in the database.

我正在开发一个Windows应用程序。我需要在win app中模拟Session(我们在web应用程序中),如果用户在一段时间内处于非活动状态,他就会被注销。用户登录状态在数据库中维护。

Any innovative ideas???

任何创新的想法???

4 个解决方案

#1


You do not need Session (or CallContext, or anything else), just a Singleton "user store" with one restriction:

您不需要Session(或CallContext或其他任何东西),只需要一个具有一个限制的Singleton“用户存储”:

After the user logged in or showed some activity, you have to save the date/time of that. Next time, when the user wants do something, just compare (lastactivity + logouttime) to the actual date/time.

用户登录或显示某些活动后,您必须保存该日期/时间。下次,当用户想要做某事时,只需将(lastactivity + logouttime)与实际日期/时间进行比较。

Outline of the process could be:

该过程的大纲可能是:

              [User login]
                   |
                   !
 [User 'store' saves user date + login time]
 [This is a singleton                      ]

                  ...

[Next time user wants to do something. The   ]
[program asks user data from the user 'store']
                   |
                   !
[If the actual time is greater than user     ]
[lastactivity + LOGOUTTIME, user cannot do it]
[If not, then update last activity           ]

UserStore can be implemented as a Dictionary and used as:

UserStore可以实现为Dictionary并用作:

// Log in
Singleton.UserStore.Add("John", new UserData( yourUserObject, DateTime.Now));

...

// Check (ie. in a property-get)
var userData = Singleton.UserStore["John"];
if (userData.LastActivityDate + _LOGOUTIME > DateTime.Now()) 
{
   throw UserAutomaticallyLoggedOut();
}
else
{
   userData.LastActivityDate = DateTime.Now();
}

#2


What comes to mind is to use a BackgroundWorker to log off the user when a timer reaches zero. This timer is reset on every action the user makes. Or it could even be hooked to mouse or keyboard events so that when the user doesn't move the mouse or isn't using the keyboard the timer counts down until it reaches the log off time.

我想到的是当计时器达到零时使用BackgroundWorker注销用户。此计时器将在用户执行的每个操作上重置。或者它甚至可以挂钩到鼠标或键盘事件,这样当用户不移动鼠标或不使用键盘时,计时器会倒计时直到达到注销时间。

#3


You could create a class in your application that has some sort of timeout that is reset every time the user interacts with the software. If the timeout is reached, the user is logged out.

您可以在应用程序中创建一个具有某种超时的类,每次用户与软件交互时都会重置该类。如果达到超时,则用户注销。

The tricky thing here is to detect user interaction. In ASP.NET is pretty easy, you essentially need to check only for page requests. In a windows forms application you will need to montor input on a much more detailed level (any key press in any text box will be a user interaction for instance). I guess the KeyPreview property together with a KeyPress event listener on the form might be a good way forward.

这里棘手的是检测用户交互。在ASP.NET中非常简单,您基本上只需要检查页面请求。在Windows窗体应用程序中,您需要在更详细的级别上进行montor输入(例如,任何文本框中的任何按键都将是用户交互)。我猜KeyPreview属性和表单上的KeyPress事件监听器可能是一个很好的方法。

#4


Well, first, what do you mean by inactive? On the Web you are trying to simulate state where there is none. However, in a client app, you get all sorts events even MouseMove. Onde idea is that you could create UserControls out of the standard input controls like TextBox, Button, and so forth and have them update some sort of timer object when events are invoked. The other is to forego the UserControl stuff and just update the timer in each event handler you create. Probably it would be simpler to just update the timer based on the MouseMove or KeyDown events on the Form itself.

那么,首先,你的意思是什么不活跃?在Web上,您试图模拟没有的状态。但是,在客户端应用程序中,即使是MouseMove也可以获得各种事件。 Onde的想法是,您可以使用TextBox,Button等标准输入控件创建UserControl,并在调用事件时让它们更新某种计时器对象。另一种是放弃UserControl的东西,只更新你创建的每个事件处理程序中的计时器。可能只是根据Form本身上的MouseMove或KeyDown事件更新计时器会更简单。


推荐阅读
author-avatar
wangnan00
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有