TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Aristide Fumagalli
NA
2
1.3k
Session Triggers (Log on/off) in a Windows 7 Service
Feb 2 2018 12:14 PM
The problem I have here might have a quick workaround but I've been stuck for a while.
I am writing a service that starts a process when a user logs in and stops it when the user logs out. In my previous tries I have tried using a loop which checked the presence of explorer.exe but it didn't work very well as the infinite loop meant the service was stuck at "starting". So I have looked into the C# on session change. Here is the code I currently have, creating text files to check if it actually works.
public
partial
class
Service1 : ServiceBase
{
public
Service1()
{
InitializeComponent();
CanPauseAndContinue =
true
;
CanHandleSessionChangeEvent =
true
;
ServiceName =
"Service1"
;
}
public
void
onDebug()
{
OnStart(
null
);
}
protected
override
void
OnSessionChange(SessionChangeDescription changeDescription)
{
EventLog.WriteEntry(
"SimpleService.OnSessionChange"
, DateTime.Now.ToLongTimeString() +
" - Session change notice received: "
+
changeDescription.Reason.ToString() +
" Session ID: "
+
changeDescription.SessionId.ToString());
System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory +
"sessionchange.txt"
);
switch
(changeDescription.Reason)
{
case
SessionChangeReason.SessionLogon:
string
[] lines = { DateTime.Now.ToShortTimeString() };
System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory +
"logged in.txt"
);
System.IO.File.WriteAllLines(@
"C:\Users\Public\test\loggedin.txt"
, lines);
break
;
case
SessionChangeReason.SessionLogoff:
string
[] lines2 = { DateTime.Now.ToShortTimeString() };
System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory +
"logged out.txt"
);
System.IO.File.WriteAllLines(@
"C:\Users\Public\test\loggedout.txt"
, lines2);
break
;
}
}
protected
override
void
OnStart(
string
[] args)
{
System.IO.File.Create(AppDomain.CurrentDomain.BaseDirectory +
"onstart.txt"
);
}
I expected this to work, but apparently not. The on start text file is being created, but out of the 4 logged on/off ones that should appear (somewhere) only loggedout.txt appears in the Public user directory after a log out. No matter how many times I shut down, log in or out the other text files for login are not being created. The time in the only loggedout.txt file create also won't update.
If it helps, I'm running this in a Virtual Machine. I have also read somewhere that SessionChange might only apply to remote log ins but I'm not sure. I'm not sure how to proceed, wether to keep trying or there is another way for services to "listen" to session changes?
Thanks for taking the time to read through.
Reply
Answers (
0
)
SharePoint 2016
wrong data coming from LIS analyzer abacus diatron 380