473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Delegates and STA problem

Hi,

I Have a for each loop that calls a method in a class that will generate the UI.

I want to call this method asynchronously. I used a delegate, but every time I do deleagte.beginInvoke() this error appears:

The calling thread must be STA, because many UI components require this.


Any ideas?
Oct 5 '09 #1
4 3044
PRR
750 Expert 512MB
Look at this thread.
Look into
Control.BeginInvoke Method
Oct 5 '09 #2
Let me explain more about the scenario;

I am using WPF.

This is in onload of the page

delegate string AsyncMethodHandler(cls_PAGESTRUCTURE.NumBtnItem obj_numBtn);

foreach (NumBtnItem obj_numBtn in obj_page.NumBtns)
{
AsyncMethodHandler caller = default(AsyncMethodHandler);
caller = new AsyncMethodHandler(CreateBtns);
caller.BeginInvoke(obj_numBtn, CallBack, null);
}

CreateBtns function will create WPF objects on the page

This is the scenario as a summary
Oct 5 '09 #3
PRR
750 Expert 512MB
I have not worked on WPF as yet. To update a windows form from another thread (non UI) you need to use delegate which will call a function, which in turn uses a control.invoke to update the UI.
Article by:Nicholas Paldino.
Oct 5 '09 #4
tlhintoq
3,525 Expert 2GB
Or you could add the STAThreadattribute to your Program.CS file.

Expand|Select|Wrap|Line Numbers
  1.         /// <summary>
  2.         /// The main entry point for the application.
  3.         /// </summary>
  4.         [STAThreadAttribute]
  5.         static void Main(string[] commandLine)
  6.         {
  7.             // Your program setup stuff here
  8.             Application.EnableVisualStyles();
  9.             //Application.SetCompatibleTextRenderingDefault(true);
  10.             App myApp = new App();
  11.             myApp.Run(commandLine);
  12.         }
  13.  
Oct 5 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jeffrey T. Smith | last post by:
Back when the new J2SE1.5 features were announced, there was a JavaLive community chat (http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html) in which Neal Gafter explains the...
0
by: Steven Brown | last post by:
I'm trying to figure out how to safely use .NET events/delegates in a thread-safe class. There are a couple problems. One is that the standard "if(EventName != null) EventName(...);" call can...
4
by: Stephen | last post by:
I am new to C# and can't get my head round what delegates are and what they are for. can anyone enlighten me?
3
by: Chua Wen Ching | last post by:
Hi there, I just read Chris Sells's article at http://www.codeproject.com/csharp/delegate_bedtime.asp?df=100&forumid=2983&select=922269#xx922269xx I wonder i can do this: 1) I want to...
15
by: Marshal | last post by:
First... let's deal with Delegates. Comments welcome. 1) Invoking a NULL delegate is annoying. ** (It should just do nothing rather than crash.) 2) It's too easy to accidently attach multiple...
14
by: Lior Amar | last post by:
Quick question about threads and delegates. I have the following scenario Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a callback Thread B Invokes a DelegateB...
4
by: AMDRIT | last post by:
I am trying to understand Delegates and where/when to use them. I can see one potential use of a delegate (on form closing, set the cancel property in the event arguments.) Does anyone have a...
4
by: Tim | last post by:
There are a set of clients who need to be notified of certain events. I have used events and delegates (publisher-Subscriber model) for the notification mechanism. All the clients register with...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.