Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6823923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:50:27+00:00 2026-05-26T21:50:27+00:00

How come my eventhandler never stops calling even after i stopped my timer? Is

  • 0

How come my eventhandler never stops calling even after i stopped my timer? Is there something wrong with my code? Please help!

I’ve included my whole code inside, if you guys could do me some help please 🙂

private void Form1_Load(object sender, EventArgs e)
    {
        //Start system
        axInRFIDCtrl1.SelectReaderFeig();
        axInRFIDCtrl1.FEInit();
        short sResult = axInRFIDCtrl1.FEOpen();
        //MessageBox.Show(sResult.ToString());

        //Start timer1
        System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
        timer1.Interval = 1000;
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Enabled = true;
        timer1.Start();

        Console.ReadLine();
    }

    public void timer1_Tick(object sender, EventArgs e)
    {
        //Get ID
        string strTagIds = string.Empty;
        int iState = 0;

        axInRFIDCtrl1.FESelect(ref strTagIds, ref iState);

        string[] strTagID = strTagIds.Split(new char[] { '|' });
        string strTag = strTagID[0];
        textBox1.Text = strTag;

        //Connection to datebase
        string c1 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Project.mdb";
        OleDbConnection con = new OleDbConnection(c1);

        //Bind button
        string txt = textBox1.Text;

        string strOleDbConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Project.mdb";
        string strSqlStatement = string.Empty;
        strSqlStatement = "SELECT * FROM jiahe WHERE [Tag ID] = '" + txt + "'";
        OleDbConnection objConnection = new OleDbConnection(strOleDbConnectionString);
        OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSqlStatement, objConnection);
        DataSet ds = new DataSet();
        objAdapter.Fill(ds);

        DataTable dt = ds.Tables[0];
        dataGridView1.DataSource = dt.DefaultView;

        if (dt.Rows.Count == 1)
        {
            string strLine = string.Empty;
            string strUser = string.Empty;

            foreach (DataRow dr in dt.Rows)
            {
                string strTags = dr["Tag ID"].ToString();
                strUser = dr["User"].ToString();
                string strAge = dr["Age"].ToString();
                string strPhoneNumber = dr["Phone Number"].ToString();

                // prepare command string
                string selectString = @"SELECT Status FROM jiahe where [Tag ID] = '" + textBox1.Text + "'";

                // 1. Instantiate a new command with command text only
                OleDbCommand cmd = new OleDbCommand(selectString, objConnection);

                // 2. Set the Connection property
                cmd.Connection.Open();

                // 3. Call ExecuteScalar to send command
                string str = cmd.ExecuteScalar().ToString();

                cmd.Connection.Close();

                foreach (DataRow datarow in dt.Rows)
                {
                    //string strName = string.Empty;
                    strName = datarow["User"].ToString();

                    if (str.Length == 2 || str.Length == 0)
                    {
                        // prepare command string
                        string updateString = @"update jiahe set Status = 'OUT' where [Tag ID] = '" + textBox1.Text + "'";

                        // 1. Instantiate a new command with command text only
                        OleDbCommand cmd1 = new OleDbCommand(updateString, objConnection);

                        // 2. Set the Connection property
                        cmd1.Connection.Open();

                        // 3. Call ExecuteNonQuery to send command
                        str = cmd1.ExecuteNonQuery().ToString();
                        cmd1.Connection.Close();

                        //write text file to outgoing spool
                        //TextWriter tw = new StreamWriter(@"C:\cygwin\var\spool\sms\outgoing\sms.txt");

                        TextWriter tw = new StreamWriter(@"C:\\Test.txt");
                        {
                            tw.WriteLine("To: 6592786618\n");
                            tw.WriteLine("\n");
                            tw.WriteLine("\n" + strName + @" has just left at " + DateTime.Now);
                            tw.Close();
                        }

                        MessageBox.Show(strName + " has left the house.");

                        //Start timer2
                        System.Windows.Forms.Timer timer2 = new System.Windows.Forms.Timer();
                        timer2.Interval = 1000 * 60 * 30; //30 mins interval
                        timer2.Tick += new EventHandler(timer2_Tick);
                        timer2.Enabled = true;
                        timer2.Start();

                        //Log to listbox
                        // Set the selection mode to multiple and extended.
                        listBox1.SelectionMode = SelectionMode.MultiExtended;
                        listBox1.BeginUpdate();
                        listBox1.Items.Add(DateTime.Now + " - " + strName + " > OUT");
                        listBox1.EndUpdate();

                        //Log event to log file
                        string cs = "Minder+Finder Event Log";
                        EventLog elog = new EventLog();

                        if (!EventLog.SourceExists(cs))
                        {
                            EventLog.CreateEventSource(cs, cs);
                        }

                        elog.Source = cs;
                        elog.EnableRaisingEvents = true;
                        elog.WriteEntry(DateTime.Now + " - " + strName + " > OUT");
                    }
                    else
                    {
                        // prepare command string
                        string updateString = @"update jiahe set Status = 'IN' where [Tag ID] = '" + textBox1.Text + "'";

                        // 1. Instantiate a new command with command text only
                        OleDbCommand cmd1 = new OleDbCommand(updateString, objConnection);

                        // 2. Set the Connection property
                        cmd1.Connection.Open();

                        // 3. Call ExecuteNonQuery to send command
                        str = cmd1.ExecuteNonQuery().ToString();
                        cmd1.Connection.Close();

                        //write text to outgoing spool
                        TextWriter tw = new StreamWriter(@"C:\\Test.txt");
                        //using (TextWriter tw = File.CreateText("C:\cygwin\var\spool\sms\outgoing\Test.txt"));
                        {
                            tw.WriteLine("To: 6592786618\n");
                            tw.WriteLine("\n");
                            tw.WriteLine("\n" + strName + @" has just returned home at " + DateTime.Now);
                            tw.Close();
                        }
                        MessageBox.Show(strName + " has returned home.");

                        //Stop timer2
                        timer2.Tick -= timer2_Tick;
                        timer2.Enabled = false;
                        timer2.Stop();

                        //Log to listbox
                        // Set the selection mode to multiple and extended.
                        listBox1.SelectionMode = SelectionMode.MultiExtended;
                        listBox1.BeginUpdate();
                        listBox1.Items.Add(DateTime.Now + " - " + strName + " > IN");
                        listBox1.EndUpdate();

                        //Log event to log file
                        string cs = "Minder+Finder Event Log";
                        EventLog elog = new EventLog();

                        if (!EventLog.SourceExists(cs))
                        {
                            EventLog.CreateEventSource(cs, cs);
                        }

                        elog.Source = cs;
                        elog.EnableRaisingEvents = true;
                        elog.WriteEntry(DateTime.Now + " - " + strName + " > IN");

                    }
                }
            }
        }
        else
        {
            timer1.Enabled = false;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.ShowDialog();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form3 form3 = new Form3();
        form3.ShowDialog();
    }

    public void timer2_Tick(object sender, EventArgs e)
    {
        MessageBox.Show(strName + " has left");

        //write text file to outgoing spool
        //TextWriter tw = new StreamWriter(@"C:\cygwin\var\spool\sms\outgoing\sms.txt");
        TextWriter tw = new StreamWriter(@"C:\\Test1.txt");
        {
            tw.WriteLine("To: 6592786618\n");
            tw.WriteLine("\n");
            tw.WriteLine("\n" + strName + @" has just left at " + DateTime.Now);
            tw.Close();
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:50:28+00:00Added an answer on May 26, 2026 at 9:50 pm

    If timer2 is an instance variable, then the problem is that you’re creating a local variable also called timer2 and starting that. You’re then stopping the member variable timer2, not the local scope timer2. The local scope timer2 will keep firing until the garbage collector gets around to disposing it.

    I’m pretty sure timer2 is an instance variable, otherwise the other references to it wouldn’t compile. Also note that because you’re creating the timer2 inside a loop, you’re probably creating a boatload of them, and all of them happily fire away until the garbage collector puts them out of their misery.

    EDIT:Here is a fairly simple example of use within a Form.

    http://www.java2s.com/Code/CSharp/GUI-Windows-Form/GUIandtimer.htm

    If you are simply dropping timer components onto your form design, then the initialization is written for you in the InitializeComponents method as in this example. Other than this though, I can’t help much more. I think there are some structural problems in the way you are trying to use the timer. All the starting and stopping, especially with timer2 in that for loop seems like it will cause you much trouble and headache.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wasn't expecting to come across this error. I imagine I'm doing something wrong
I've come across a problem, that is probably seen pretty often, but has never
In our legacy VB6 code we used a system timer to perform a callback
I've recently come up with some code that logic (and various places on the
I want to scan a queue every 10 seconds using a Timer. If there
I come across this problem when i am writing an event handler in SharePoint.
Come across what looks at first sight like an MT-issue, but I'm trying to
I come from a Java background, where packages are used, not namespaces. I'm used
I come from the Microsoft world (and I come in peace). I want to
How come this doesn't work (operating on an empty select list <select id=requestTypes></select> $(function()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.