abduls85
abduls85

Reputation: 548

Question to Auto logout C# desktop application

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();
    }

}

Upvotes: 0

Views: 1822

Answers (2)

jcolebrand
jcolebrand

Reputation: 16025

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
}

Upvotes: 0

Jim Mischel
Jim Mischel

Reputation: 134005

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
}

Upvotes: 2

Related Questions