6 Replies - 421 Views - Last Post: 13 April 2012 - 01:04 PM Rate Topic: -----

#1 heenan0420  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 07-October 11

Problem with system.timers

Posted 13 April 2012 - 06:52 AM

we are using a system.timers.timer to log out a user if the program idles for too long. the timer works on the main Form but once a button is clicked an brings up the second level form the user will be logged out after the idle time. any ideas on how to get the timer to refresh on the main form so that the user stays logged in if the mouse moves or key down is used. been struggling with this problem for the whole week any help that works will be appreciated thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with system.timers

#2 heenan0420  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 07-October 11

Re: Problem with system.timers

Posted 13 April 2012 - 08:23 AM

here is my form code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
using System.Data.SqlClient;

namespace KeyCommTester
{
    public partial class WCSBase : Form
    {
        public static System.Timers.Timer tmrUserLogout;
        public DataSet dsControlsToRoles = new DataSet();
        public DataTable dtControlsToRoles = new DataTable();
        public static string sCurrentUserName = "";
        public static string sCurrentRoleName = "";

        public WCSBase()
        {
            InitializeComponent();
        }

        public static void ResetThisTimer()
        {
            KeyCommTester.tmrUserLogout.Stop();
            KeyCommTester.tmrUserLogout.Start();

        }

       
        public void SetControlsPermissions(Form f, string sRole)
        {
            KeySql trySQL = new KeySql();
            trySQL.KeySQLMain();
            trySQL.GetConnected(trySQL.KeyConnection);

            dtControlsToRoles.TableName = "ControlsToRoles";
            dtControlsToRoles.Clear();

            SqlDataAdapter dataAdapter = null;
            string sFormName = f.Name.ToString();

            string sSql = "Select [FKRole] from [KeyWCS].[dbo].[ControlsToRoles] where [RoleName] = '" + sRole + "'";
            int iRoleID = trySQL.KeySQLGetIntField(trySQL.KeyConnection, sSql);

            //get the roleid from the database

            dataAdapter = new SqlDataAdapter("Select * from [KeyWCS].[dbo].[ControlsToRoles] where [FKPage] = '" + sFormName + "' AND [FKRole] <= " + iRoleID + "", trySQL.KeyConnection);


            //dataAdapter = new SqlDataAdapter("Select * from [KeyWCS].[dbo].[ControlsToRoles] where [FKPage] = '" + sFormName + "' AND [RoleName] = '" +  sRole + "'", trySQL.KeyConnection);
            dataAdapter.Fill(dsControlsToRoles, "ControlsToRoles");
            dtControlsToRoles = dsControlsToRoles.Tables[0];

            foreach (Control c in f.Controls)
            {
                c.Enabled = true;
                c.Visible = true;
            }
            if (iRoleID != 0)
            {

                foreach (DataRow row in dtControlsToRoles.Rows)
                {
                    string sControlName = row["FKControlID"].ToString();
                    if (!f.Controls.ContainsKey(sControlName)) continue;
                    if (row["Invisible"].ToString() == "1")
                    {
                        f.Controls[sControlName].Visible = false;
                    }
                    if (row["Disabled"].ToString() == "1")
                    {
                        f.Controls[sControlName].Enabled = false;
                    }
                }

                trySQL.CloseKeySQL();
            }
        }


        private void WCSBase_MouseDown(object sender, MouseEventArgs e)
        {

            if (sCurrentUserName.Length < 1)
            {
                UserLogin myLogin = new UserLogin();
                myLogin.ShowDialog();
                myLogin.Dispose();

            }
            else
            {

                Console.WriteLine(e.Button);
                tmrUserLogout.Enabled = true;
                tmrUserLogout.Stop();
                tmrUserLogout.Start();
            }

        }

        private void WCSBase_KeyDown(object sender, KeyEventArgs e)
        {
            if (sCurrentUserName.Length < 1)
            {
                //if (UserLogin.ActiveForm.WindowState == FormWindowState.Normal) return;
                UserLogin myLogin = new UserLogin();
                myLogin.ShowDialog();
                myLogin.Dispose();
            }
            else
            {
                ResetThisTimer();               
            }

        }

        private void WCSBase_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            Console.WriteLine(e.KeyCode);
            tmrUserLogout.Stop();
            tmrUserLogout.Start();


        }

        private void WCSBase_MouseMove(object sender, MouseEventArgs e)
        {
            if (KeyCommTester.sCurrentUserName.Length > 0)
            {
                ResetThisTimer();
            }

        }

        private void WCSBase_Load(object sender, EventArgs e)
        {

            tmrUserLogout = new System.Timers.Timer(60000);
            tmrUserLogout.Enabled = true;
            tmrUserLogout.AutoReset = true;
            tmrUserLogout.Elapsed += new ElapsedEventHandler(tmrUserLogout_Tick);           
            if (this.DesignMode) return;
            if (sCurrentUserName.Length < 1)
            {
                UserLogin myLogin = new UserLogin();
                myLogin.ShowDialog();
                myLogin.Dispose();
            }

            SetControlsPermissions(this, sCurrentRoleName);
        }

        //public void tmrUserLogout_Tick(object sender, EventArgs e)
        //{
        //    Console.WriteLine(tmrUserLogout);
        //    UserLogin myLogin = new UserLogin();
        //    myLogin.UserTimedLogout();
        //}

        private void WCSBase_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
        }

        private void tmrUserLogout_Tick(object sender, EventArgs e)
        {
            Console.WriteLine(tmrUserLogout);
            UserLogin myLogin = new UserLogin();
            myLogin.UserTimedLogout();
        }


    }
}


Was This Post Helpful? 0
  • +
  • -

#3 Robin19  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 206
  • View blog
  • Posts: 436
  • Joined: 07-July 10

Re: Problem with system.timers

Posted 13 April 2012 - 08:55 AM

Form2 should raise an event when a button is pressed or the mouse moves. Form1 can listen for that event and reset the timer every time the event is triggered.
Was This Post Helpful? 1
  • +
  • -

#4 heenan0420  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 07-October 11

Re: Problem with system.timers

Posted 13 April 2012 - 09:22 AM

well every form inherits from this form the problem i am havin is once a new from is opened from the base form during run time it will not reset the timer for the original form that logs in the user and logs them out
Was This Post Helpful? 0
  • +
  • -

#5 Robin19  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 206
  • View blog
  • Posts: 436
  • Joined: 07-July 10

Re: Problem with system.timers

Posted 13 April 2012 - 12:12 PM

        private void WCSBase_Load(object sender, EventArgs e)
        {

            tmrUserLogout = new System.Timers.Timer(60000);
        }

If the child form inherits the base form, it also inherits this method call. The original tmrUserLogout doesn't go away. It still runs along and ticks. All this line does is create another tmrUserLogout and assigns it to the variable. When Reset() is called, it only effects one of the timers. I tested this by putting a MessageBox.Show method in the timer tick. I was soon having screen fulls of messageboxes every time it ticks.

Add this before creating the timer and see if it helps.
        private void WCSBase_Load(object sender, EventArgs e)
        {
            // stop current timer
            if (tmrUserLogout != null)
                 tmrUserLogout.Dispose();

            tmrUserLogout = new System.Timers.Timer(60000);
        }

Was This Post Helpful? 1
  • +
  • -

#6 heenan0420  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 07-October 11

Re: Problem with system.timers

Posted 13 April 2012 - 12:54 PM

Oh this is awesome thank you a ton we have been working an this all week long and you just saved our life. Seriously I am adding Kudos to you for real!! Thank you sooo much again you dont know how much time you just saved me!!
Was This Post Helpful? 0
  • +
  • -

#7 heenan0420  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 07-October 11

Re: Problem with system.timers

Posted 13 April 2012 - 01:04 PM

have another question for you if your up to it.
on the mousedown event it works as long as the user clicks out in the form where there is nothing but wont work when the user clicks on the button. any suggestions for that, it is called in the same coding that i posted before and if you have any suggestions that would be great! and if you could point me to a place to where i could add to your kudos i will be more than happy to recomend you for more or add to it, thanks in advance!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1