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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:28:51+00:00 2026-05-10T16:28:51+00:00

I have a scenario. (Windows Forms, C#, .NET) There is a main form which

  • 0

I have a scenario. (Windows Forms, C#, .NET)

  1. There is a main form which hosts some user control.
  2. The user control does some heavy data operation, such that if I directly call the UserControl_Load method the UI become nonresponsive for the duration for load method execution.
  3. To overcome this I load data on different thread (trying to change existing code as little as I can)
  4. I used a background worker thread which will be loading the data and when done will notify the application that it has done its work.
  5. Now came a real problem. All the UI (main form and its child usercontrols) was created on the primary main thread. In the LOAD method of the usercontrol I’m fetching data based on the values of some control (like textbox) on userControl.

The pseudocode would look like this:

CODE 1

UserContrl1_LoadDataMethod() {     if (textbox1.text == "MyName") // This gives exception     {         //Load data corresponding to "MyName".         //Populate a globale variable List<string> which will be binded to grid at some later stage.     } } 

The Exception it gave was

Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on.

To know more about this I did some googling and a suggestion came up like using the following code

CODE 2

UserContrl1_LoadDataMethod() {     if (InvokeRequired) // Line #1     {         this.Invoke(new MethodInvoker(UserContrl1_LoadDataMethod));         return;     }      if (textbox1.text == "MyName") // Now it won't give an exception     {     //Load data correspondin to "MyName"         //Populate a globale variable List<string> which will be binded to grid at some later stage     } } 

But it still seems that I’ve come back to square one. The Application again becomes unresponsive. It seems to be due to the execution of line #1 if condition. The loading task is again done by the parent thread and not the third that I spawned.

I don’t know whether I perceived this right or wrong.

How do I resolve this and also what is the effect of execution of Line#1 if block?

The situation is this: I want to load data into a global variable based on the value of a control. I don’t want to change the value of a control from the child thread. I’m not going to do it ever from a child thread.

So only accessing the value so that the corresponding data can be fetched from the database.

  • 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. 2026-05-10T16:28:51+00:00Added an answer on May 10, 2026 at 4:28 pm

    As per Prerak K’s update comment (since deleted):

    I guess I have not presented the question properly.

    Situation is this: I want to load data into a global variable based on the value of a control. I don’t want to change the value of a control from the child thread. I’m not going to do it ever from a child thread.

    So only accessing the value so that corresponding data can be fetched from the database.

    The solution you want then should look like:

    UserContrl1_LOadDataMethod() {     string name = "";     if(textbox1.InvokeRequired)     {         textbox1.Invoke(new MethodInvoker(delegate { name = textbox1.text; }));     }     if(name == "MyName")     {         // do whatever     } } 

    Do your serious processing in the separate thread before you attempt to switch back to the control’s thread. For example:

    UserContrl1_LOadDataMethod() {     if(textbox1.text=="MyName") //<<======Now it wont give exception**     {         //Load data correspondin to "MyName"         //Populate a globale variable List<string> which will be         //bound to grid at some later stage         if(InvokeRequired)         {             // after we've done all the processing,              this.Invoke(new MethodInvoker(delegate {                 // load the control with the appropriate data             }));             return;         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The very basic idea is simply to put a bug… May 11, 2026 at 8:27 pm
  • Editorial Team
    Editorial Team added an answer Forgive me if I'm missing something, but checking for a… May 11, 2026 at 8:27 pm
  • Editorial Team
    Editorial Team added an answer I just had something similar to this - my question… May 11, 2026 at 8:27 pm

Related Questions

I have several .NET Windows Forms applications that I'm preparing to convert into a
I want to create an application which will have a client and server components.
I work on a thick-client app that often runs into issues accessing network shares.
Are there some help resources, or can anyone give me a brief Idea how

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.