473,796 Members | 2,632 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Single-threaded apartment?

I've build an application that monitors a folder. When a new file is
created in that folder it kicks off a process. On such process is to
load a form. When I get to the statement to create and instance of the
form, I get this error message:

---------------------------
ActiveX control '243f57b7-dd96-4179-865d-27ac7b5df609' cannot be
instantiated because the current thread is not in a single-threaded
apartment.
---------------------------

The form is one that I've created and contains various active x
controls. It is on the initialization of these controls that it crashes.
How can I make this work?

Thanks,
Dustin
Jan 11 '07 #1
6 16332
I think the form needs to be the primary thread. Can you create
the form and hide it, have it kick off the thread that monitors
the folder, and when that kicks back an event, show your form?

Robin S.

"Dustin Davis" <du****@davisvi llage.comwrote in message
news:eX******** ******@TK2MSFTN GP02.phx.gbl...
I've build an application that monitors a folder. When a new file is
created in that folder it kicks off a process. On such process is to
load a form. When I get to the statement to create and instance of the
form, I get this error message:

---------------------------
ActiveX control '243f57b7-dd96-4179-865d-27ac7b5df609' cannot be
instantiated because the current thread is not in a single-threaded
apartment.
---------------------------

The form is one that I've created and contains various active x
controls. It is on the initialization of these controls that it
crashes. How can I make this work?

Thanks,
Dustin

Jan 11 '07 #2

Dustin Davis wrote:
I've build an application that monitors a folder. When a new file is
created in that folder it kicks off a process. On such process is to
load a form. When I get to the statement to create and instance of the
form, I get this error message:

---------------------------
ActiveX control '243f57b7-dd96-4179-865d-27ac7b5df609' cannot be
instantiated because the current thread is not in a single-threaded
apartment.
---------------------------

The form is one that I've created and contains various active x
controls. It is on the initialization of these controls that it crashes.
How can I make this work?

Thanks,
Dustin
Well, if this is a separate exe, then you need to make sure you have
the <STAThreadattri bute on you sub main. The desinger normally does
this for you.

If you are creating this as a separate thread, then you need to
explicitly set that thread's ApartmentState property to
ApartmentState. STA - before you call it's start method :)

--
Tom Shelton

Jan 11 '07 #3
I think the problem is that everything is in the main form (frmMain).
This form kicks off the FileSystemWatch er, when the FileSystemWatch er
triggers the event, it is calling function also within frmMain.

Do I need to create a new routine that lanches frmMain and the
FileSystemWatch er separately? If that's the case it make take a while as
I will have to probably pull a lot of function out of frmMain and make
them public.

RobinS wrote:
I think the form needs to be the primary thread. Can you create
the form and hide it, have it kick off the thread that monitors
the folder, and when that kicks back an event, show your form?

Robin S.

"Dustin Davis" <du****@davisvi llage.comwrote in message
news:eX******** ******@TK2MSFTN GP02.phx.gbl...
>I've build an application that monitors a folder. When a new file is
created in that folder it kicks off a process. On such process is to
load a form. When I get to the statement to create and instance of the
form, I get this error message:

---------------------------
ActiveX control '243f57b7-dd96-4179-865d-27ac7b5df609' cannot be
instantiated because the current thread is not in a single-threaded
apartment.
---------------------------

The form is one that I've created and contains various active x
controls. It is on the initialization of these controls that it
crashes. How can I make this work?

Thanks,
Dustin

Jan 11 '07 #4

Dustin Davis wrote:
I think the problem is that everything is in the main form (frmMain).
This form kicks off the FileSystemWatch er, when the FileSystemWatch er
triggers the event, it is calling function also within frmMain.

Do I need to create a new routine that lanches frmMain and the
FileSystemWatch er separately? If that's the case it make take a while as
I will have to probably pull a lot of function out of frmMain and make
them public.
Dustin - if the call is comming back from another thread, you need to
mashal the call to the method on the main form. You do that by calling
the method through the forms Invoke method. You can test if this is
required in the event by checking the Forms.InvokeReq uired property...

--
Tom Shelton

Jan 11 '07 #5
Tom - I'm not sure I understand this:
you need to
mashal the call to the method on the main form. You do that by calling
the method through the forms Invoke method.
Can you give me more information on this?

Thanks,
Dustin
Tom Shelton wrote:
Dustin Davis wrote:
>I think the problem is that everything is in the main form (frmMain).
This form kicks off the FileSystemWatch er, when the FileSystemWatch er
triggers the event, it is calling function also within frmMain.

Do I need to create a new routine that lanches frmMain and the
FileSystemWatc her separately? If that's the case it make take a while as
I will have to probably pull a lot of function out of frmMain and make
them public.

Dustin - if the call is comming back from another thread, you need to
mashal the call to the method on the main form. You do that by calling
the method through the forms Invoke method. You can test if this is
required in the event by checking the Forms.InvokeReq uired property...
Jan 11 '07 #6

Dustin Davis wrote:
Tom - I'm not sure I understand this:
you need to
mashal the call to the method on the main form. You do that by calling
the method through the forms Invoke method.

Can you give me more information on this?
You have a couple of potential issues here.

1) your using AX controls that are expecting to be run on an sta
thread. This should be ok, as long as you access them on the same
thread they were created, because the IDE will make the main
application thread an sta thread by default (it will apply the
STAThread attribute to the sub main).

2) You can not safely access a windows forms control on any thread
other then the one on which it was created. That means you need to
mashal all access of the form or it's controls back to the main thread.
You can do this through the Invoke method that all controls inherit.
Here is a link to a fairly good series of articles on the topic:

http://msdn2.microsoft.com/en-us/library/ms951089.aspx

It's the code is in C# - but the principals are the same in VB as well.
It might be helpful if you were to post a minimal set of code, just
to show the basic architecture - then it would be easier to point out
exactly what changes to make :)

--
Tom Shelton

Jan 12 '07 #7

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

Similar topics

5
8262
by: sinister | last post by:
The examples in the online manual all seem to use double quotes, e.g. at http://us3.php.net/preg_replace Why? (The behavior is different with single quotes, and presumably simpler to understand.)
12
5128
by: Dennis Plöger | last post by:
Hi all! I'm currently having some problems parsing a char array in c++. (And yes, I'm a half-newbie ;-)) Perhaps you can help me with this: #include <iostream> using std::cout; void outchar(char *outcharstring)
5
11459
by: Joel | last post by:
Hi, I incorporated a function in my code that whenever I use a string variable in an sql statement if the string contains a single quote it will encase it in double quotes else single quotes. Queston: How do you handle a string that contains both single & double quotes (i.e. 12'X7") Here's the function:
11
535
by: Thom Little | last post by:
I would like three states on an icon ... Left Click Right Click Double Click Left Click is fired at least once on a Double Click Is there a good example that shows how to determine if the user entered a Left Click or a Double Click?
3
3645
by: R Millman | last post by:
under ASP.NET, single stepping in debug mode appears not to stop within event procedures. i.e. 1) Create web page with submit button and event procedure for the click event in the code behind page, 2) Breakpoint in the Page_Load, 3) debug the web page and click the submit button, 4) "step into" under debug several times, 5) The debugger does not stop at any of the statements in the click event handler. A breakpoint is needed in each...
2
9768
by: John Dann | last post by:
I'm retrieving some columns from a database with numeric values that fall comfortably within the range of the Single type and I'm tempted to use Single for the relevant column type in the retrieved dataset. (Actually the values are held as integers in the database for compact storage but are scaled to their true Single values during retrieval). But I'm sure that I keep reading that there's no performance penalty to using Double rather...
13
4285
by: Kevin Walzer | last post by:
Which of the Windows/Unix package builders for Python applications is capable of creating single-file executables? I'm thinking of: 1. py2exe 2. Mcmillan Installer/PyInstaller 3. cxfreeze The apps I've seen created by py2exe aren't single-file at all, the install folder is full of files besides the main program. I'm looking for a solution that stuffs all libraries, scripts, and the Python
7
12240
by: ashley.ward | last post by:
We have been using VB6 to develop small custom apps that access an Oracle database, in order to extend a larger product that is developed by our colleagues in Germany (who use C++ and Java). As each app is small and simple, we have been distributing each to the customer in the form of a single EXE file. I have been attempting to build another one of these small custom apps, using VB 2005 Express Edition. It doesn't seem to be possible...
3
2744
by: Eric Layman | last post by:
Hi, I've saved data into the db by doing a replace() on single quote. Right now on data display on a datagrid, it shows double single quote. How do I make changes during run time of datagrid so that the double single quote will be replaced as single quote? Pls advise.
0
8634
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be anything from a short integer value to a complex struct type, also has a pointer to the next node in the single-linked list. That pointer will be NULL if the end of the single-linked list is encountered. The single-linked list travels only one...
0
9533
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10190
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10019
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9057
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.