[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

11/07/2009 at 07:20AM PST, ID: 24880469
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.2

VB.NET Console App with FileSystemWatcher

Asked by jacksonm1234 in Microsoft Visual Basic.Net, .NET Framework 2.x, Visual Basic Programming

Tags: Vb.net, console, delegates, threading, threads, filesystemwatcher

Hi 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!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
Private Delegate Sub ShowForm(ByVal motion As Boolean)
Sub Main()
        MsgBox(System.Threading.Thread.CurrentThread.ManagedThreadId)
        Console.Title = "Watcher"
        Dim watcher As New FileSystemWatcher("C:\Users\Mitch\Desktop\My Documents")
        watcher.NotifyFilter = NotifyFilters.FileName
        AddHandler watcher.Created, AddressOf FolderEvent
        watcher.EnableRaisingEvents = True
        While Console.ReadLine <> "exit" 
        End While
    End Sub  
'Here's where I tried using delegates to invoke the form
Private Sub FolderEvent(ByVal source As Object, ByVal e As FileSystemEventArgs)         
MsgBox(System.Threading.Thread.CurrentThread.ManagedThreadId)
        If e.ChangeType = WatcherChangeTypes.Created Then
            If e.Name = "capture.txt" Then
                Dim sForm As New ShowForm(AddressOf ShowCam) 
                sForm.Invoke(False)
                'Dim wcp As New WebCamPreview(False)
                'wcp.ShowDialog()
.
.
.  
Public Sub ShowCam(ByVal motion As Boolean)
        MsgBox(System.Threading.Thread.CurrentThread.ManagedThreadId)
        Dim wcp As New WebCamPreview(motion)
        wcp.ShowDialog()
    End Sub
[+][-]11/07/09 08:36 AM, ID: 25766904

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual Basic.Net, .NET Framework 2.x, Visual Basic Programming
Tags: Vb.net, console, delegates, threading, threads, filesystemwatcher
Sign Up Now!
Solution Provided By: x77
Participating Experts: 1
Solution Grade: C
 
 
[+][-]11/07/09 09:09 AM, ID: 25767094

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/07/09 09:20 AM, ID: 25767139

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/07/09 09:46 AM, ID: 25767217

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/07/09 10:21 AM, ID: 25767379

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/07/09 10:34 AM, ID: 25767450

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-91 - Hierarchy / EE_QW_3_20080625