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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:09:21+00:00 2026-05-25T20:09:21+00:00

I am working on a multi-threaded application which needs to update a Winforms control

  • 0

I am working on a multi-threaded application which needs to update a Winforms control as it progresses (A DataGridView). In order to prevent multiple access to the same shared resource, I started out with the following lock structure:

if (DGV.Columns.Count >= DesiredColumnCount) return;
lock(DGV)
{
    while (DGV.Columns.Count < DesiredColumnCount)
    {
        DGV.Columns.Add(newColumn);
    }
}

I then realized that, since the DGV was created on the UI thread, it needs to be .Invoke()‘d. This changes the code to:

if (DGV.Columns.Count >= DesiredColumnCount) return;
lock(DGV)
{
    DGV.Invoke(new MethodInvoker(() =>
        {
            while (DGV.Columns.Count < DesiredColumnCount)
            {
                DGV.Columns.Add(newColumn);
            }
        }
}

My question is: Isn’t this redundant? the lock will block the worker thread until it has exclusive access to DGV, and the Invoke() will block the worker thread until the UI thread can pick up the invoke request and execute the code. Can’t I just get by using only the Invoke()?

(That is the main question. Of course if there are any other multithreading sins in the above code, please comment)

  • 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-25T20:09:22+00:00Added an answer on May 25, 2026 at 8:09 pm

    It is a bit redundant, the Invoke call will “dispatch” the operpation to the UI thread. Each subsequent Invoke call will be dispatched in a serial fashion, so there isn’t really any need to do any locking.

    You may consider using BeginInvoke instead of Invoke order to prevent the worker thread from blocking, but again, that will be done “serially” so there is no need to worry about locking.

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

Sidebar

Related Questions

I'm currently working on a multi-threaded application, and I occasionally receive a concurrently modification
In a multi-threaded application I'm working on, we occasionally see ConcurrentModificationExceptions on our Lists
I'm working on a multi-threaded Silverlight application. The application has two threads: Main/UI and
I'm working on a multi-threaded win32 MFC application. We are rendering a map and
I'm working on a multi-monitor kiosk application that needs to run full-screen on both
I'm working on a multi-threaded Qt application and would like to connect a signal
I have a multi-threaded Delphi 6 Pro application that I am currently working on
I'm developing a multi-threaded ASP.NET 3.5 application, during working with some file, I'm getting
I'm working on a multi-threaded C# Windows application that makes frequent calls into a
I am currently working on a multi-threaded game application for the Android platform... so

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.