Hi,
I have created a service which calls a method called RUN() which is inside another class library project in which i have written code,in such a way that whenever a lock or unlock or logon event occurs it should store the time of occurence of that event inside a notepad.But the events are not getting triggered at all.But when i have written the same code inside the service itself rather than calling events from a class library events are getting triggered .
could anybody explain me why is it happening like that when i have written the same events code in service its working fine but if i write that code in different class library and call the method inside service the events are not triggering.
i have searched vigourously in google but iam not able to find any solution for it.
please find below the code i have written in c# class library and calling the run() method on onstart() of the service.
<pre lang="c#">
public void run()
{
SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SysEventsCheck);
}
public void SysEventsCheck(object sender, SessionSwitchEventArgs e)
{
switch (e.Reason)
{
case SessionSwitchReason.SessionLock:
Note("sessionlock");
break;
case SessionSwitchReason.SessionUnlock:
Note("sessionUnlock");
break;
case SessionSwitchReason.SessionLogoff:
Note("Logoff");
break;
case SessionSwitchReason.SessionLogon:
Note("Logon");
break;
}
}
public void Note(string s)
{
try
{
string fileloc = Path.Combine(@"C:\", "ORCAError");
if (!Directory.Exists(fileloc))
{
Directory.CreateDirectory(fileloc);
}
string filepath = Path.Combine(fileloc, "TestEvents.txt");
using (StreamWriter sw = new StreamWriter(filepath, true))
{
sw.WriteLine(s + " at {0}" + " " + DateTime.Now.ToString());
}
}
catch { }
}
below is the code i have written in service to call the run() method in class library
public partial class DesktopLogMonitorPro : ServiceBase
{
public DesktopLogMonitorPro()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Class1 c = new Class1();
c.Run();
while (true)
{
}
}
}