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

  • SEARCH
  • Home
  • 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 5941075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:03:33+00:00 2026-05-22T16:03:33+00:00

I need to implement an auto logout feature in C#. Previously i have asked

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

}

  • 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-22T16:03:34+00:00Added an answer on May 22, 2026 at 4:03 pm

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I need to implement an auto-logout feature for WordPress. With JavaScript this seems
I have to implement auto-logout functionality in one of my projects and i just
Any one know how to implement auto send message using MFMessageComposer....i mean No need
My requirement is to implement auto complete feature in my rails application. I need
I need to implement auto-capitalization inside of a Telerik RadEditor control on an ASPX
I need to implement an in-memory tuple-of-strings matching feature in C. There will be
I need to implement auto-increment for each nightly builds. For this, i need to
I'm trying to implement a browser auto-complete feature for my application Login view. However
I have to implement a feature where the last position of the window is
I need to implement a simple logging mechanism for my web application. I have

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.