473,405 Members | 2,262 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,405 software developers and data experts.

[StaThread]

JJ
Hello NG

I am making a dll. in that dll i set reference to the System.Windows.Forms
to be able to get the OpenFileDialog.
But when I use the :

If ( OFD.ShowDialog() == System.Windows.Forms.DialogResult.OK )

I get the error:
"Current thread must be set to single thread apartment (STA) mode before OLE
calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it. This exception is only raised if a debugger is attached to the
process."

How to prevent this error, and to open the open file dialog.

Kind regards.
Johnny E. Jensen
Oct 16 '08 #1
1 1640
On Wed, 15 Oct 2008 23:09:01 -0700, JJ <jj@jj.jjwrote:
[...]
"Current thread must be set to single thread apartment (STA) mode before
OLE
calls can be made. Ensure that your Main function has STAThreadAttribute
marked on it. This exception is only raised if a debugger is attached to
the
process."

How to prevent this error, and to open the open file dialog.
In the thread that will use the dialog, before you do anything that would
initialize COM (preferably just as the very first thing), call
Thread.SetApartmentState(ApartmentState.STA).

That _should_ allow that thread to safely create and use OpenFileDialog.

Pete
Oct 16 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: sonu | last post by:
Hi I have developed a smart client application. When i try to execute it. It displays the first form which is the login screen. On giving the corrent login id and password, the main form opens....
11
by: warren | last post by:
Hello, Anyone can brief me what is this in front of the Main method for? and when must it be there, and when is it optional? thank you.
3
by: Justine | last post by:
hi all, i just want to know the significance of in the C# application. Why & for What reason is this used. Thanz in Advance, Justine
9
by: | last post by:
Hi All, I have allready tried to ask a similar question , but got no answer until now. In the meantime, I found, that I cannot understand some thread-settings for the Main() function . If I use...
1
by: Alberto | last post by:
What's the meaning of the STAThread attribute? Thanks
2
by: Tom | last post by:
Do we need to put the STAThread attribute on our Sub Main anymore if we are using the 1.1 Framework? See some YEAs and NEAs when searching on Google so thought I would ask here. Tom
4
by: garyusenet | last post by:
I am at a loss with this. I tried to go back to basics, and start learning all i didn't understand, starting at the top of the code file generated by VS. But I can't seem to get any sort of start...
12
by: rafalK | last post by:
Hi All, I have a big problem with STAThread attribute. I'm using XNA framework connected with WinForms. XNA is working in non STAThread. I have a problem with displaying CommonDialog forms e.g....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.