Session variable in C# desktop application? - c#

I am developing a C# a stand alone single user desktop application that requires the user to login to the application. I want to ensure that when there is no activity for 5 minutes or so the application will prompt the user to login again. I have several solution in mind to do this but there do not seem efficient. Previously while doing web programming i was able to do this kind of feature using session variable are there are similiar kind of features in C# that can be used for desktop application.

One way to do this is to set a 5-minute timer that is always running, and logs the user out when it ticks. Then you can have any activity restart the timer from the beginning.
If this is a WinForms app, you can have your top-level forms implement IMessageFilter. In your PreFilterMessage function you would restart the timer and return false for messages that indicate activity (WM_KEYDOWN, WM_MOUSEMOVE, etc.) to let everything get processed normally.

You can always add a trigger to auto-logout within a Windows Forms Application. Here is a link with examples with an accepted answer
How can I trigger an auto-logout within a Windows Forms Application?
To monitor user activity, you could create a custom Form-based class
from which your application forms will inherit. There you can
subscribe to the MouseMove and KeyDown events (setting the KeyPreview
property to true), either of which will be raised whenever the user is
active. You can then create a System.Threading.Timer, with the due
time set to 30 minutes, and postpone it using the Change() method
whenever user activity is detected.

No, but session state is just a list of variables to help overcome the stateless nature of web applications. Since desktop applications are not stateless, there's no need. I'd just use a simple timer or something similar and log the user out after 5 minutes of inactivity.

Related

Windows Runtime notifications within DispatchTimer Event

I have a simple xaml form which, when LoadState is called, enables a Dispatch Timer. When the dispatch timer fires, it checks a service for new items. If a new item meets a certain criteria, it adds the message to a local variable (which ends up being bound to a listview), and we use the ToastNotification framework to create a new toast. However, one thing I've noticed is that the ToastNotifications appear only to work when the app is focused (which, of course, defeats the point). I think this makes sense, in part, because my app is suspended when not focused.
However, I know my DispatchTimer is executed when the app is suspended, because I see the web requests firing off to the service. It must mean that my call to send the notification is deferred somehow. I guess conceptually, how do I get the toast notifications to execute if my app is suspended? I have read up on BackgroundTasks, but those only respond to system events. What I really want is for my app to poll for messages regardless of its suspended state, update the UI when it can and update notifications whether the app is supended or not.
Unfortunately the only reliable way to achieve your scenario is with a BackgroundTask, that you can run every 30 minutes. DispatcherTimer requires the UI thread, which will only run if your app has focus. If you are seeing web requests go out it may be due to some other anomaly, but effectively when your app is suspended no further user code is guaranteed to run - including popping a toast.
It sounds like you might be looking for something more like push notifications. Would these work for you? You could use them to display toast notifications, update your live tile etc.
Notification Platform Development on Windows
Mobile Push Notifications to Any Client with Azure Notification Hubs
Get started with push notifications in Mobile Services

Web and windows forms application using same db issues

We have got a web application and a windows forms application. The web application contains a download link to download this forms application.
This forms application will check the client machine privileges and drive space etc. and will update the values to a global database using some webservice calls.
So the web application will continuously checking the global database for the status ie how much checking has been completed by the forms application.
So as soon as the download popup appears for the forms application to download, the web application will start to check the status. But if a user cancels the download or if a user close the forms application, then in these cases how the web application can stop checking for status.
But in my case the web application will always check for the status change , even if the client cancels the download or closes the forms application. So how to avoid this?
I think a timeout is your only real option here. Basically the web side will only check for a certain amount of time before giving up. Your status table in your database should include a "last update timestamp" field. If the status is not "complete", and the current time is greater than that timestamp by X seconds, the webserver assumes the process has timed out.
You could try to have some thing like a switch as part of a record or some thing in the Database, so the very next time your web app tries to read lets say the bit field it would know that it should stop checking. You could control this bit field as ON & OFF via your forms application. Then your web application you can setup a polling mechnism that checks the db every so often before timing itself off all together based on the bit field or if not been done then aftera period of time.
Having said that your forms application would need to be able to call the outside world where your db is some where located and update it, it can be done many ways, web service call, http, ect...
Update:
I apologies for the delay in replying but did you understand what I said? If your winforms application can call your web service then it can tell it to store a flag field some where like a record in the db for example that your web-application is polling and checking and then by setting that flag field your web-application would know to stop doing any thing with that record, item.
As you mentioned you are worried about:
User clicking cancel on download:
In this scenario you would not set the checking of your record by your web-application unless the user has downloaded and run your win-forms application for the first time, so dont start checking upon dowload of the win-forms application but start checking once for the first time the user has opened it, you can do this upon start of the winforms application by setting the flag field from your winforms app by calling your web-service. You will need a polling mechanism on top of this every so often, like a service.
When the user closes the winforms application
In this case you would upon termination/close of your winforms app call your web service and set the flag field not to check that record, item any more.
You will need polling in any case as I am thinking you will have many users and hence you will need to monitor the db for incoming messages from your winforms app. Also please be aware as some users are behind firewalls, limited security permissions on their machines and on private networks and your winforms app may not always be able to call your web service.
Hope that helped.

Running a program when Windows Boots (before Login)

I wonder is it possible to run my application before login on windows.? Its actually a WinForm application(Preferably other than Windows service).
I mean my application must be running even before log in and it should sit on System Tray from which I can "show" or open Interface for user.
To have:
Something happen between system startup and user login
An icon in the notification area for users to interact with
You must break your application up into two separate components. One of these will be a WinForms/WPF application which provides the notification area icon and allows the user to interact with it. The other will be a Windows Service, or possibly a scheduled task.
Once you've done this, the windows service can focus on doing whatever needs to be done prior to logon and the notification area application can assume responsibility for showing the results, or whatever else to the end user.
To answer the question you posed in comments regarding reading data from the Event Log, yes this is possible. There are other questions on here which will give you that particular answer:
read the windows event log by particular Source
Read event log in C#
This MS article might help but it is a bit old:
http://support.microsoft.com/kb/142905
Hopefully it'll put you on the right tracks though.
I think, it doesn't make sense, to acquire user input before a user has logged into the system. So, if the application needs input from a user, why start it before the use has logged in? If the application just starts some background work, than you should use a windows service, as this is the prefered way in windows.
Type in run gpedit.msc, for Group Policy,
There you can set start up script.
Application will launch before Windows Login,
Step to produce :-
Start --> Run --> gpedit.msc --> Local Computer Policy --> Windows Settings --> Script (Startup/ShutDown),
Add you .exe
It will launch Before login.
Do not try more in Group Policy, it may happen harmful for System
By Programmatic logic,
Try with registry key
this value is updating in registry,
by our program we can update directly registry then we can call application
You can schedule any application to be run when computer is powered on using Windows Task Scheduler. There is a corresponding option there.
But why would you need this? You should use a service for this.

Desktop notifier with no visible form to user

I want to make a application that runs with no form interaction with user and only specific person can run a form and change config setting. The application is desktop reminder which runs every 4 months and shows up a notification.
I don't have any idea how to start it. Please guide me with some good suggestions.
If you have an application that needs to notify the user once every 4 months, its a bit overhead to have it running all the time.
Use the Task Scheduler in Windows, to schedule this command to run (once per day, or every week) check if the condition is met. If not silently exit.
You can effectively create a windows Service and then configure it to allow it interact with the desktop through the Services administration console.
However, for security reasons in W2008 Server (and I assume that more or less it will be the same with W7 or even Vista, you'll have to try it) this behaves differently, and services that interact with the desktop are not allowed. Actually, I remember that when I created and showed a Window I got a notification and when clicking on it the desktop switched to another one with my window. Still, no issue with XP, I've done it.

How to run a .NET application when the system becomes idle?

I have created a .NET Windows application in C#. I want to execute this application when the system becomes idle for sometime. The application is a user login application, i.e. when system becomes idle for sometime, a login page appears where the user logins with his username and password. Then only the user can continue using the system.
How can I make it work out? Please help as I am new in Windows applications.
I thought to run the application as a Windows service. I managed to run the application on Windows service start. But how can I trigger the app to run only when system goes to idle in Windows service? I also need to start the timer after the user logins to check idle time again. Please provide some code examples.
The system already comes with such functionality. On the screen saver configuration page you simply check the box titled "On resume, display logon screen". It can be configured with group policy.
There's absolutely no point re-implementing this.
How about:
Start your form hidden
Get the system idle time
Show your window.

Categories

Resources