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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:29:54+00:00 2026-05-21T00:29:54+00:00

References: http://msdn.microsoft.com/en-us/library/ms171728.aspx http://stackoverflow.com/questions/5408155/how-to-make-delegate-thread-sta I wanted to create a new Thread and make it STA

  • 0

References:
http://msdn.microsoft.com/en-us/library/ms171728.aspx
http://stackoverflow.com/questions/5408155/how-to-make-delegate-thread-sta

I wanted to create a new Thread and make it STA therefore I was not able to use asynchronous delegates or BackgroudWorker (as mentioned in references link 1) Therefore I end up creating a Thread of my own make it STA and attach a callback to know when the Task is complete. The code is something like below and even though I am using invoke required, I still get InvalidOperationException (once in a while)


delegate UpdateEventHander(Object sender, EventArgs e);
class MyTask{
   // to generate an event
   public event UpdateEventHandler Finished;
   public void Start(){
        Result = // something that require the thread to be STA.
        Finished(this, EventArgs.Empty);
   }
   public Result GetResult(){
        return Result;
   }
}

Class Foo : Form{
   // It has many UI Controls obviously
   public void doSomething(){
      MyTask task = new MyTask();
      task.Finished += new UpdateEventHander(CompletionHandler);
      Thread thread = new Thread(new ThreadStart(task.Start));
      thread.setAppartmetnState(AppartmentState.STA);
      thread.start();
   }
   public void CompletionHandler(Object sender, EventArgs e){
      MyTask task = (MyTask) sender;
      if (oneOfMyControls.InvokeRequired){
         delegateToUpdateUIconrols del = new delegateToUpdateUIconrols(updateUIControls);
         del.invoke();
      }else{
         UpdateUIControls();
      }
   }
   public delegate void delegateToUpdateUIconrols();
   public void UpdateUIControls(){
       // It updates UI controls
       // Datagrid view value properties like backgroud color and stuff.
       // change text in the label.
   }

}

Question 1: Which thread will UpdateUIControls execute ? – if you say “Main UI Thread” – then in that case how will the system know if its supposed to run in Main UI thead and NOT some OTHER thread? I am not passing any reference (about Main UI thread) when I call invoke() .. so invoke() is technically executed on the same thread..

Question 2: Once in a while, I get the InvalidOperationException. Exactly this one
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/6b450a21-e588-414a-afae-9adabfd03674/

If UpdateUIControls is executing in the main UI thread, there should be not prblem, Right? So, I guess answer to my question really depends upon question 1.

I will appreciate if someone share his/her wisdom on this

Karephul

  • 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-21T00:29:55+00:00Added an answer on May 21, 2026 at 12:29 am

    Controls have thread-affinity; you can only safely talk to them from their creating thread.

    You are checking InvokeRequired; however, you are mixing up Delegate.Invoke (runs on the current thread) with Control.Invoke (runs on the UI thread); very different meaning. It should be:

    oneOfMyControls.Invoke(del [, args]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On the page http://msdn.microsoft.com/en-us/library/bb202066.aspx If references something called POLL Driver V2 however this is
I am trying to follow this example: http://msdn.microsoft.com/en-us/library/aa179614%28SQL.80%29.aspx# It says to add the following
In msdn i found this - http://msdn.microsoft.com/en-us/library/ff650218.aspx but I don't know where to find
According to this http://www.cplusplus.com/reference/clibrary/csignal/signal.html SIGINT is generally used/cause by the user. How do i
I overloaded the 6 signals listed on this site http://www.cplusplus.com/reference/clibrary/csignal/signal.html Then i ran my
I am trying to enable these debugging preferences (NSShowAllViews, NSShowAllDrawing) as shown here: http://developer.apple.com/technotes/tn2004/tn2124.html#SECAPPKITVIEWS
My quick search reveals the reference implementation ( http://stax.codehaus.org ), the Woodstox implementation (
I'm interested in using a Reed-Solomon error correction [ http://en.wikipedia.org/wiki/Reed%E2%80%93Solomon_error_correction as a reference] to
What references offer a good summary/tutorial for using RDF/OWL? There seem to be enough
Most references I've seen, and my IDE's code completion all have my specifying a

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.