Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
                    // Console.WriteLine("sessionlock" + " " +DateTime.Now);
                    Note("sessionlock");
                    break;
                case SessionSwitchReason.SessionUnlock:
                    //Console.WriteLine("sessionUnlock" + " " + DateTime.Now);
                    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

C#
public partial class DesktopLogMonitorPro : ServiceBase
{
    public DesktopLogMonitorPro()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {

        Class1 c = new Class1();
            c.Run();
            while (true)
            {
            }
    }


}
Posted
Updated 4-Aug-15 23:16pm
Comments
satti_ananthreddy 6-Aug-15 8:20am    
Any body please help me...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900