That makes sense, but is there any way to do this in a console application, where the filesystemwatcher is not in a form?
Main Topics
Browse All TopicsHi everyone,
I have a console app that I'm using to monitor a folder using the filesystemwatcher class. When a new file is created in this folder, it should load a form, take a picture from the webcam, copy it to the clipboard, and save the file to the HD. Here's the problem, if I load the form straight from my Main method (i.e. without using the filesystemwatcher events), it works perfectly. If I use the filesystemwatcher events, however, I get this error when trying to use the clipboard:
"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."
After some messing around with the ManagedThreadId property of the CurrentThread, I'm able to see that the thread ID of Main is 9 (or whatever), and the filesystemwatcher events and the form is on thread ID 10. I'm guessing that I need to get the form to load from thread ID 9 in order for this to work properly.
I'm not good with delegates, but I've been trying everything I can think of, and I can't seem to get back to thread 9! Help!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
To use the same thread, you do no need invoke.
The simple mode is :
Declare a variable bFindFile = False
Replace the While loop to Wait for bFindFile changes.
When bFindFile, then show the Form and reset bFindFile.
Alter the variable bFindFile on FolderEvent when the File condition applies.
You nedd to change the Console.ReadLine to a function for inspect the keyboard and not blocking or create a thread to readline, then end the application.
Note that the process, now is waiting by Console.Readline, then you can not use it to show the form.
I Test your original with begininvoke and it works.
it run a process - ReadLine.
Next, when file created a new Trhead. This Thread is from S.O. call, you can terminate the function, you can not show the form in it.
Then You run a new thread to create the form and return (Return Control to FileSystemWatcher).
In the third thread, yo can show the form.
Business Accounts
Answer for Membership
by: x77Posted on 2009-11-07 at 08:36:53ID: 25766904
I use Form1 with a buton to exit the application.
as delegate, use
BeginInvoke(New MethodInvoker(AddressOf ShowCam))
This use the same Message loop as form1.
Select allOpen in new window