Best type of c# solution

I'm pretty new to c# and could do with some help.  I want to create a solution that simply looks to see if a ProcessID has stopped running.  If it has, it logs the user out.  

Fellow EE members have been helping out with code suggestions (thank you), but I am curious as to what type of solution I should build it in.  I want it to have as small a footprint as possible.  It doesn't need any forms/messages, it is simply loaded, presented with the PID (via a command line argument), and then periodically checks to see if it's still running.  If it isn't, it logs the user out.

But the main point of the question is the lightest way to build it (using c#)
Andy BrownDeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Use GetProcessByID() and WTSDisconnectSession as wtsdisconnectsession (wtsapi32).

E.g. untested, unstructured and needs error handling:
namespace ConsoleCS
{
    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;

    public class Program
    {
        [DllImport("wtsapi32.dll", SetLastError = true)]
        static extern bool WTSDisconnectSession(IntPtr hServer, int sessionId, bool bWait);

        const int WTS_CURRENT_SESSION = -1;
        static readonly IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;

        public static void Main(string[] args)
        {
            Process byId = Process.GetProcessById(Convert.ToInt32(args[0]));
            Console.WriteLine(byId.ProcessName);
            if (byId != null)
            {
                if (!WTSDisconnectSession(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, false))
                {
                    throw new Win32Exception();
                }
            }

            Console.WriteLine("Done.");
            Console.ReadLine();
        }
    }
}

Open in new window


p.s. without digging deeper into it, sounds like a PowerShell solution should be also evaluated.
1
Flabio GatesCommented:
Cobbling together an example based on this documentation, I have:
namespace ConsoleApplication1
{
    using System;
    using System.Management;

    class Program
    {
        static void Main(string[] args)
        {
            int processID = int.Parse(args[0]);
            WaitForProcessToEnd(processID);
        }

        private static void WaitForProcessToEnd(int processID)
        {
            const int wmiPollingInSeconds = 5;

            string queryString = string.Format(
                "SELECT * FROM __InstanceDeletionEvent WITHIN {0} WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.ProcessId={1}", 
                wmiPollingInSeconds, 
                processID
                );

            WqlEventQuery eventQuery = new WqlEventQuery(queryString);
            ManagementEventWatcher eventWatcher = new ManagementEventWatcher { Query = eventQuery, Scope = new ManagementScope("root\\CIMV2") };

            ManagementBaseObject mbOobj = eventWatcher.WaitForNextEvent();
            eventWatcher.Stop();

            Console.WriteLine("Process with ProcessId={0} has ended.", processID);
        }
    }
}

Open in new window


Oh and I do not claim that this is "best." Am just giving you options.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
Sounds like you need a service that listens for user login events.  When the user is logged in, the service can then monitor the processes that the user starts.  When the user starts access, the service can then monitor that process.  When the process exits, the user is then logged out.  I have a service that I wrote for a similar task, if you want I can share it later (currently at work and do not have access to the service logic at home).

-saige-
0
Andy BrownDeveloperAuthor Commented:
Thank you everyone.  Based initially on Flabio's suggestion, I ended up creating a small c# console application that is called when the main application starts.  It looks for the process ID every 15 seconds.  If it's no longer running, it simply logs the user off.

It's pretty lightweight and seems to work well on initial tests - so thank you again for all of the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.