热门标签 | 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事件更新计时器会更简单。


推荐阅读
  • 深入了解 Windows 窗体中的 SplitContainer 控件
    SplitContainer 控件是 Windows 窗体中的一种复合控件,由两个可调整大小的面板和一个可移动的拆分条组成。本文将详细介绍其功能、属性以及如何通过编程方式创建复杂的用户界面。 ... [详细]
  • 本文介绍了如何在C#中启动一个应用程序,并通过枚举窗口来获取其主窗口句柄。当使用Process类启动程序时,我们通常只能获得进程的句柄,而主窗口句柄可能为0。因此,我们需要使用API函数和回调机制来准确获取主窗口句柄。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 本文详细探讨了KMP算法中next数组的构建及其应用,重点分析了未改良和改良后的next数组在字符串匹配中的作用。通过具体实例和代码实现,帮助读者更好地理解KMP算法的核心原理。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 探讨如何通过编程技术实现100个并发连接,解决线程创建顺序问题,并提供高效的并发测试方案。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • c# – UWP:BrightnessOverride StartOverride逻辑 ... [详细]
  • CMake跨平台开发实践
    本文介绍如何使用CMake支持不同平台的代码编译。通过一个简单的示例,我们将展示如何编写CMakeLists.txt以适应Linux和Windows平台,并实现跨平台的函数调用。 ... [详细]
  • 本文深入探讨了 Java 编程语言的基础,特别是其跨平台特性和 JVM 的工作原理。通过介绍 Java 的发展历史和生态系统,帮助初学者理解如何编写并运行第一个 Java 程序。 ... [详细]
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社区 版权所有