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 8219627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:14:16+00:00 2026-06-07T13:14:16+00:00

Is this the best way to utilize a status message that displays for some

  • 0

Is this the best way to utilize a status message that displays for some time?

private void SetStatus(Color _color, string _msg)
{
    System.Threading.Thread t = new System.Threading.Thread(() =>
    {
        stsStatusMsg.ForeColor = _color;
        stsStatusMsg.Text = _msg;
        System.Threading.Thread.Sleep(2000);
        stsStatusMsg.Text = "";
    });
    t.Start();
}

Example of calling it:

SetStatus(Color.Red, AppMessages["msgALREADYIN"]);

stsStatusMsg (which is a ToolStripStatusLabel) is accessed only by this method during the whole application operation.

The above code actually works. The message is displayed on the control for 2 secs and then goes away…

  • 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-06-07T13:14:18+00:00Added an answer on June 7, 2026 at 1:14 pm

    If this is in WinForms, as I suspect, this is not likely to work. You are only allowed to access the UI controls from the main windows message thread, which can be reached by using Invoke on a Control. Update, while your code does work, this is because the actual object you are manipulating from the thread is not a Control – in general in WinForms, cross thread access is not allowed.

    Also, a new thread for each status seems overkill. Yes, the thread is going to be disposed by the GC at some point, but Threads are an expensive resource to create, and your code creates one for each status change. A much better approach is to use the built-in thread pool (where you acquire processing time on a long-lived thread from a pool managed by the framework), or using a Timer, which is designed for running code at specified intervals.

    How about changing the text, then use a Timer to hide it again after the desired time ?

    Here is a solution using a Timer:

    private void SetStatus(Color _color, string _msg)
    {
        stsStatusMsg.ForeColor = _color;
        stsStatusMsg.Text = _msg;
        timer.Start();
    }
    
    private void StatusTimer_Elapsed(object sender, EventArgs e)
    {
        stsStatusMsg.Text = "";
        timer.Stop();
    }
    

    The above code assumes that the timer is constructed already, and that it has it’s Tick event hooked up to the StatusTimer_Elapsed event. The reason why the System.Windows.Forms.Timer class is well suited for this, is that the timer event is raised on the UI thread, meaning you will not have to worry about cross-thread access to the controls.

    Disclaimer: I just thought up the above sample without using a compiler, it might need to be tweaked.

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

Sidebar

Related Questions

Ok so let me try to explain this the best way that i can.
This question ( Best way to strip punctuation from a string in Python )
Is this the best way to convert String hex to bytes? Or can you
This question is related to this one: Best way to store product colors in
Okay I'll try describe this the best way I can. I have a chat
I am not sure if I am going about this the best way, but
This is the best way I can think of phrasing this question, given this
I am wondering if this is the best way to go about solving my
I'm not sure if this is the best way to do it, but I
I'm not entirely sure if this is the best way to word it, however,

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.