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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:25:51+00:00 2026-06-13T16:25:51+00:00

i need some clarifications about the multithreading programming model, becuase, rarely i have to

  • 0

i need some clarifications about the multithreading programming model, becuase, rarely i have to deal with it (mostly all the time i do web-programming).

Antecedents

i have a program that i have made in C# basically it’s a data monitor and execute pooling every specific ammount of time over the database, let’s say 1 minute.

How this application is relatively simple i decided use a monolithic structure,
i made the all the GUI in range of classes, and i made an object that deals with the connection and data retrieving.

to deal with the requeriment of the pooling time i used a System.Timers.Timer, which is basically a Thread wrapping with a firing timed event, here i executed the data pooling.

When new data is found i need to fire a method in X class that belongs to the GUI (now running in the main thread) from the function triggered by the timer (in other thread by definition).

the first time i tried to call the method directly to the method triggered by the timer, resulting in a cross-thread error: because the method resides in X class and was not contructed in the timer thread.

Then i tried again using other approach. I created a event with his respective delegate in the class that read the data and subscribe the GUI objects to that event.

Problem

now when i fire the event .net produces an exception saying me that the sender object instance is null, exactly don’t know why, but i suspects that is because the object itself it’s created with the GUI objects in the main thread and when i decide to send the object from the other thread (that it’s generating the event), the instance simply doesn’t exists because wasn’t created there.

This is the function called when the timer expires:

    private void PoolearMensajes(object Sender, ElapsedEventArgs e)
    {
        try
        {
            pRdr.getPendingMessages(); <--- This function creates the exception
            if (pRdr.MensajesPendientes > 0)
            {
                this.oSysIcon.Text = pRdr.MensajesPendientes.ToString() + " tickets pendientes";
                this.oSysIcon.BalloonTipIcon = ToolTipIcon.Info;
                this.oSysIcon.BalloonTipText = pRdr.MensajesPendientes.ToString() + " tickets pendientes";
                this.oSysIcon.BalloonTipTitle = "Información";
                this.oSysIcon.ShowBalloonTip(3000);
                System.Media.SystemSounds.Exclamation.Play();
            }
        }
        catch (Exception ex)
        {
            this.oSysIcon.BalloonTipTitle = "Error";
            this.oSysIcon.Text = "Ha ocurrido un error.";
            this.oSysIcon.BalloonTipIcon = ToolTipIcon.Error;
            this.oSysIcon.BalloonTipText = ex.Message;
            this.oSysIcon.ShowBalloonTip(3000);
            System.Media.SystemSounds.Exclamation.Play();
        }
    }

Here is the method resides in the data retriever object that was created in main thread:

    public delegate void TicketsFoundHandler(object sender, TicketEventArgs ta);
    public event TicketsFoundHandler TicketsFound;

    public void getPendingMessages()
    {
        string sSQL;
        sSQL = " an SQL Query that counts rows;";
        oCmd = new SqlCommand(sSQL, oConn);
        oConn.Open();
        this.i_pendmsg = (int)oCmd.ExecuteScalar();
        if (this.i_pendmsg > 0)
        {
            TicketEventArgs ta = new TicketEventArgs("", this.i_pendmsg);
            TicketsFound(this, ta); <---- ERROR!
        }
        oConn.Close();
    }

The Error message it’s: Object reference not set to an instance of an object.

any ideas , any improvements, or something i haven’t cosidered ?

  • 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-13T16:25:52+00:00Added an answer on June 13, 2026 at 4:25 pm

    the first time i tried to call the method directly to the method
    triggered by the timer, resulting in a cross-thread error: because the
    method resides in X class and was not contructed in the timer thread.

    You were almost there. You just need to call BeginInvoke() to invoke your operation on the GUI thread.

    You might also find that using a BackgroundWorker is even easier.

    EDIT:

    Your specific error in your question is because nothing has subscribed to your TicketsFound event. Use TicketsFound += ... to do so. Also, check for null before raising the event:

    if (TicketsFound != null) TicketsFound(...)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would need some clarification about JPA/Hibernate inheritance. Say I have an abstract Fruit
I need some clarifications about the X Server and the applications. As described in
I need some clarification. I have a Reportwriter dll that uses Crystal Reports. It
Just need some quick clarification I have 2 Queries in my Access Database that
Need some regular expressions help. So far I have my code working to allow
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
Need some help with Activerecord Querying in a has_many :through association. Model: Job class
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
I'm in need of some clarification. I've been reading about REST, and building RESTful

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.