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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:27:27+00:00 2026-06-02T00:27:27+00:00

Yet another question about updating from background threads. To get to the point: In

  • 0

Yet another question about updating from background threads.

To get to the point: In the application, background threads need to update UI. I’ve considered using an in-between collection to buffer messages and have a timer to display them. At the moment we are trying a simplest approach.

Code attempt #1:

void foo(string status)
{
    if (this.InvokeRequired)
    {
        BeginInvoke(new MethodInvoker(delegate()
        {
            InsertStatusMessage(status);
        }));

    }
    else
    {
        InsertStatusMessage(status);
    }  
}

This seems to have some flaws. Msdn states that InvokeRequired also returns false if the window handle hasn’t been created yet (not available, in my opinion). So the code should be:

void foo(string status)
{
    if (this.InvokeRequired)
    {
        BeginInvoke(new MethodInvoker(delegate()
        {
            InsertStatusMessage(status);
        }));

        // wait until status is set
        EndInvoke(result);
    }
    else if(this.IsHandleCreated)
    {
        InsertStatusMessage(status);
    }
    else
    {
        _logger.Error("Could not update status");
    } 
}

The code above somehow also throws (for an unknown and not replicated reason). We use DevExpress and this is the unhandled exception message (no information nor any clue on what/where the error happened):

System.NullReferenceException: object reference not set to an instance
of an object in
DevExpress.Utils.Text.FontsCache.GetFontCacheByFont(Graphics graphics,
Font font) in
DevExpress.Utils.Text.TextUtils.GetFontAscentHeight(Graphics g, Font
font) in
DevExpress.XtraEditors.ViewInfo.BaseEditViewInfo.GetTextAscentHeight()
in
DevExpress.XtraEditors.ViewInfo.TextEditViewInfo.CalcTextBaseline(Graphics
g) in
DevExpress.XtraEditors.ViewInfo.BaseControlViewInfo.ReCalcViewInfo(Graphics
g, MouseButtons buttons, Point mousePosition, Rectangle bounds) in
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo.UpdateCellEditViewInfo(GridCellInfo
cell, Point mousePos, Boolean canFastRecalculate, Boolean calc) in
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo.CreateCellEditViewInfo(GridCellInfo
cell, Boolean calc, Boolean allowCache) in
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo.RequestCellEditViewInfo(GridCellInfo
cell) in
DevExpress.XtraGrid.Views.Grid.Drawing.GridPainter.DrawRegularRowCell(GridViewDrawArgs
e, GridCellInfo ci) in
DevExpress.XtraGrid.Views.Grid.Drawing.GridPainter.DrawRegularRow(GridViewDrawArgs
e, GridDataRowInfo ri) in
DevExpress.XtraGrid.Views.Grid.Drawing.GridPainter.DrawRow(GridViewDrawArgs
e, GridRowInfo ri) in
DevExpress.XtraGrid.Views.Grid.Drawing.GridPainter.DrawRows(GridViewDrawArgs
e) in
DevExpress.XtraGrid.Views.Grid.Drawing.GridPainter.DrawContents(GridViewDrawArgs
e) in
DevExpress.XtraGrid.Views.Grid.Drawing.GridPainter.Draw(ViewDrawArgs
ee) in DevExpress.XtraGrid.Views.Base.BaseView.Draw(GraphicsCache
e) in DevExpress.XtraGrid.GridControl.OnPaint(PaintEventArgs e)
in System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs
e, Int16 layer, Boolean disposeEventArgs) in
System.Windows.Forms.Control.WmPaint(Message& m) in
System.Windows.Forms.Control.WndProc(Message& m) in
DevExpress.XtraEditors.Container.EditorContainer.WndProc(Message& m)
in DevExpress.XtraGrid.GridControl.WndProc(Message& m) in
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
in System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)

I want to use Begin/End Invoke instead of Invoke because it requires less stuff (method delegates) and it is more readable.

What have I missed, how can I safely do thread invoking? I just want to add a message in a listbox. I really don’t care if the calling thread will waits for a few milliseconds.

  • 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-02T00:27:31+00:00Added an answer on June 2, 2026 at 12:27 am

    You can call directly “Invoke” with “MethodInvoker”.

    void foo(string status)
    {
        Invoke(new MethodInvoker(() => {InsertStatusMessage(status);}));
    }
    

    I used this also with DevExpress controls (especially to async update the data sources on several Xtragrids on one form).

    For more information about MethodInvoker there is an excellent post.

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

Sidebar

Related Questions

Here goes a yet another SQL question about dates… I'm building a calendaring application
Yes, I know, yet another question about mutable objects. See this for general background
yet another basic question ... I have an app that takes about 4-5 secs
I posted another question earlier about getting the arguments from a context menu App.
This is not yet another question about the difference between abstract classes and interfaces,
Yet another question about classes as Im new to OOP. I am creating a
Sorry for yet another question about EF4 context lifespan, but I've been wondering about
Yet another simple question about unit testing model objects that uses data access layer.
I guess this question will sound familiar, but I am yet another programmer baffled
First, I am NOT trying to spur yet another debate about LINQ vs. stored

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.