ASP.Net Developer
ASP.Net Developer

Reputation: 331

automatic logoff application when pc not used C# windows application

i want to make an winform app that is if any one is not using computer for eg for 10minutes then it should display a popup (Are you present) if not then PC will automatically LOG off .

Kindly give me some code or idea for getting movement of mouse and keyboard key press i.e. if mouse or keyboard are not used for 10minutes then this popup should displayed.... help me please thanks

This code i used but it is not working. i used 4seconds for che

 Timer t = new Timer();
    string x;
    string y;
    string z;

    private void Form1_Load(object sender, EventArgs e)
    {
        z = transfer();
        t.Interval = (4000);
        t.Enabled = true;

        t.Tick += new EventHandler(timer1_Tick);
        t.Start();
    }




    string transfer()
    {
        x = Cursor.Position.X.ToString();
        y = Cursor.Position.Y.ToString();
        return x+y;           
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        try
        {
            x = Cursor.Position.X.ToString();
            y = Cursor.Position.Y.ToString();
            string p = x + y;
            if (z == p)
            {

                MessageBox.Show("Are you present", "Alert");
              Process.Start(@"C:\WINDOWS\system32\rundll32.exe", "user32.dll,LockWorkStation");
            }
            else
            {
                t.Stop();
                this.Form1_Load(this, e);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }

}

Upvotes: 0

Views: 1466

Answers (1)

geedubb
geedubb

Reputation: 4057

Your logic seems a little confused. I wouldn't advise firing your form load repeatedly. You could try something like this, which will fire some code if the user does not move their mouse for 4 seconds:

    Timer t = new Timer();
    Point currPos;
    Point oldPos;

    private void Form1_Load(object sender, EventArgs e)
    {
        currPos = Cursor.Position;
        t.Interval = (4000);
        t.Enabled = true;

        t.Tick += new EventHandler(timer1_Tick);
        t.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        try
        {
            currPos = Cursor.Position;
            if (oldPos == currPos)
            {
                t.Stop();
                // I'm not clear what you want here - perhaps remove the messagebox and lock the workstation?
                var res = MessageBox.Show("Are you present", "Alert");
                if (res == DialogResult.OK)
                {
                    t.Start();
                }
                // Process.Start(@"C:\WINDOWS\system32\rundll32.exe", "user32.dll,LockWorkStation");
            }
            oldPos = currPos;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }

Upvotes: 1

Related Questions