0

I need to implement an auto logout feature in C#. Previously i have asked a similiar question before and i managed to implement it using the System.Windows.Forms.Timer . But right now i have a additional requirement apart from resetting the timer when the user move the mouse or enters a key i also need to reset the timer when a new message is received via the serial port ( DataReceived event handler ).

serialPort.DataReceived += port_DataRecieved;

I need to include the reset function in a portion of the port_DataRecieved function. I cannot simply add another delegate method to the serialPort.DataReceived which will perform the reset as the serialPort.DataReceived will received a lot of other messages that i am not interested in. I want to perform a reset when the message that i am interested in arrives. And i know where to put the reset feature. The issue is that the timer does not reset in port_DataRecieved method. And i cannot achieve the desired result using the System.Threading.Timer. Anyone can guide me or provide some suggestion on this issue ? Any help provided will be greatly apperciated.

public partial class Form1: Form
{  

    private System.Windows.Forms.Timer sessionTimer = new System.Windows.Forms.Timer();

    public Form1()
    {
        initialiseTimer(); 
    } 

    private void port_DataRecieved(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
            serialPort= (SerialPort)sender;
            str = serialPort.ReadExisting();
            string[] split = str.Split(Convert.ToChar(10));
            for (int i = 1; i < split.Length; i++)
            {
                str = split[i];
                if (split[i].StartsWith("+CMTI:"))
                {
                    sessionTimer.Stop();
                    sessionTimer.Start();

                    //Other codes
                }
            }
        }
        catch (Exception)
        {
            MessageBox.Show("Error processing received commands !", "CONNECTION ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            sendRecPort.Close();
        }
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        sessionTimer.Stop();
        sessionTimer.Start(); 
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        sessionTimer.Stop();
        sessionTimer.Start(); 
    }

    private void initialiseTimer()
    {
        sessionTimer.Interval = (5 * 60 * 1000);        
        sessionTimer.Tick += new EventHandler(logOutUser);
        sessionTimer.Stop();
        sessionTimer.Start();
    }

    private void logOutUser(object sender, EventArgs e)
    {
        // logout the user
        this.Hide();
        //Open up the login Form
        login.Show();
    }

}

Community
  • 1
  • 1
abduls85
  • 548
  • 8
  • 15

2 Answers2

2

Your problem is that the the DataReceived event is being executed on a thread other than the UI thread. You're trying to modify the timer (a UI object) from a non-UI thread. This typically throws an exception, but it's possible that the method that issues the DataReceived event is swallowing that exception.

From the documentation for the DataReceived event:

The DataReceived event is raised on a secondary thread when data is received from the SerialPort object. Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception. If it is necessary to modify elements in the main Form or Control, post change requests back using Invoke, which will do the work on the proper thread.

You need to synchronize with the UI thread to set the timer.

void ResetTimer()
{
    sessionTimer.Stop();
    sessionTimer.Start();
}

private void port_DataRecieved(object sender, SerialDataReceivedEventArgs e)
{
    //Other codes
    this.Invoke((MethodInvoker)delegate { ResetTimer(); });
    //Other codes
}
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
0

I need to include the reset function in a portion of the port_DataReceived function.

Ok. Gotcha.

I cannot simply add another delegate method to the serialPort.DataReceived which will perform the reset as the serialPort.DataReceived will receive a lot of other messages that I am not interested in.

Ok, but I thought you said:

I want to perform a reset when the message that I am interested in arrives.

So you either have to listen to that DataReceived method, or you won't know when that message arrives.

I'm confused. What is it you want to do? Magic?

if (dataReceived == "someValue1") 
{
  //action if matches "someValue1"
}
else if (dataReceived.Contains("someValue2"))
{
  // action if contains "someValue2"
}
else if (dataReceived.IndexOf("someValue3") != -1 )
{
  // action if contains "someValue3"
}
else if (dataReceived == "someValue4")
{
  // action if matches "someValue4"
}
else
{
  // default action
}
Gustavo Mori
  • 8,319
  • 3
  • 38
  • 52
jcolebrand
  • 15,889
  • 12
  • 75
  • 121
  • A lot of messages tends to arrive in the serial port that i am listening on. I just want to perform a reset when messages that begins with +CMTI arrives and not perform the timer reset when other messages are detected. – abduls85 May 19 '11 at 17:00
  • this is why we have switch statements. – jcolebrand May 19 '11 at 17:00
  • Sorry i know what a switch statement is and can you enlighten me on how it can help with my current problem. Thanks in advance. – abduls85 May 19 '11 at 17:03
  • He means you have to parse the data being received, and use an if statement to reset when it applies. – Marcelo May 19 '11 at 17:11
  • sorry for the late reply. I was fixing some issues with my sim card on my gsm modem. I will work on this problem tomorrow. Actually sorry if i confuse you guys actually i already did the coding to filter the message that is of interest to me and put the reset timer code. The problem is just that the timer does not reset even though the code is executed. That is what that puzzles me. I have updated my post above. – abduls85 May 19 '11 at 17:56