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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:42:21+00:00 2026-05-16T11:42:21+00:00

please consider the following scenario for .net 2.0: I have an event that is

  • 0

please consider the following scenario for .net 2.0:

I have an event that is fired on system.Timers.Timer object. The subscriber then adds an item to a Windows.Forms.Listbox upon receiving the event. This results in a cross-thread exception.

My question is what would be the best way to handle this sort of situation. The solutions that I have come up with is as follows:

private   delegate void messageDel(string text);
private   void ThreadSafeMsg(string text)
{
  if (this.InvokeRequired)
  {
    messageDel d = new messageDel(ThreadSafeMsg);
    this.Invoke(d, new object[] { text });
  }
  else
  {
  listBox1.Items.Add(text);
  listBox1.Update();
  }
}

 // event
void Instance_Message(string text)
{
  ThreadSafeMsg(text);
}

Is this the optimum way to handle this in .net 2? What about .net 3.5?

  • 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-16T11:42:22+00:00Added an answer on May 16, 2026 at 11:42 am

    There’s no point in using Control.InvokeRequired, you know that it always is. The Elapsed event is raised on a threadpool thread, never the UI thread.

    Which makes it kinda pointless to use a System.Timers.Timer, just use the System.Windows.Forms.Timer. No need to monkey with Control.Begin/Invoke, you can’t crash your program with an ObjectDisposedException when the event is raised just as the user closes the form.

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

Sidebar

Related Questions

Please consider the following scenario. I have a Task, that returns an object, lets
Please consider the following three .NET types: I have an interface, an abstract class,
Please consider the following scenario. I have a form with a property: class MyForm
Please consider the following: http://jsfiddle.net/CASM6/9/ I have two rows of tabs. there are two
please consider the following: I have a queue of objects represented as an array.
I want to create an android app. please consider the following scenario: Iam standing
Please consider following example: The source image consists of 6 areas that need to
Please consider the following scenario, There are two web applications App1 & App2. A
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,

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.