Note that there are some explanatory texts on larger screens.

plurals
  1. POc# stop eventhandler after timer stops
    primarykey
    data
    text
    <p>How come my eventhandler never stops calling even after i stopped my timer? Is there something wrong with my code? Please help!</p> <p>I've included my whole code inside, if you guys could do me some help please :)</p> <pre><code>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 + " &gt; 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 + " &gt; 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 + " &gt; 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 + " &gt; 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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload