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

VB.NET 08 OpenFileDialog in 2nd Thread throws ThreadStateException

11
Hi,

I have a main form which creates a thread. I want to make this child thread open a OpenFileDialog, but whenever I try to do this I get:

ThreadStateException.

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.


Clearly it doesn't like it when I show an OpenFileDialog box from a secondary thread, but is there any way around this?

Here's some sample code that illustrates the problem:

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  3.         trd = New Threading.Thread(AddressOf trdTask)
  4.         trd.IsBackground = True
  5.         trd.Start()
  6.     End Sub
  7.  
  8.     Private trd As Threading.Thread
  9.     Private Sub trdTask()
  10.         Dim file As OpenFileDialog = New OpenFileDialog
  11.         file.ShowDialog()
  12.     End Sub
  13. End Class

Thanks,

Wagz
May 13 '08 #1
2 2805
Plater
7,872 Expert 4TB
Did you follow its directions?
Is your program running with STAThread and not MTAThread(or whatever).
Did you confirm that the exception is only thrown when the debugger is attached?
Why do you need it runing on another thread? I would think that generally the user interactive stuff would be run on the same thread. (ie: the GUI and any interactive IO), while anything that needs to be "still going on" would be done on other threads.
May 13 '08 #2
jhaxo
57
Do you intend this file dialog to be accessible associated with a main ui?

Windows does not much like any threads other than the ui thread touching its gui's and will throw an exception if you try it. That is really a good thing, previous versions of windows let you try to do it anyway and only later would problems show up.

Try having the thread invoke a delegate in the ui that pops up this dialog?
May 13 '08 #3

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

Similar topics

5
by: Adam Clauss | last post by:
I am creating a plugin for MS Outlook. Upon clicking a toolbar button, I create series of threads (variable number each time). Within each thread, I start a form using Application.Run(...). This...
9
by: mareal | last post by:
I have noticed how the thread I created just stops running. I have added several exceptions to the thread System.Threading.SynchronizationLockException System.Threading.ThreadAbortException...
4
by: hullnwm | last post by:
In my asp.net page I'm attempting to use the OpenfileDialog object to offer a common dialog box for the user to select a particular file on their hard drive that may subsequently be uploaded to...
22
by: Brett | last post by:
I have a second thread, t2, that errors out and will stop. It's status is then "Stopped". I try to start t2 from thread 1, t1, by checking If t2.threadstate = "Stopped" Then t2.start() ...
2
by: Mark Denardo | last post by:
I'm trying to abort a suspended thread, but I get a ThreadStateException: An unhandled exception of type 'System.Threading.ThreadStateException' occurred in mscorlib.dll Additional...
5
by: De kessé | last post by:
Im trying to do this from ASP.NET and it does not work. Dim ofd As New OpenFileDialog Openfiledialog is not there. But I can find it in the object browser. In my web config i can see this...
8
by: marcus.kwok | last post by:
I am having a weird problem and I have can't figure out why it is happening. I create an OpenFileDialog and set a filename filter. When the dialog first opens, the filter works correctly, and...
8
by: Adam Honek | last post by:
This really is my unlucky day it seems. I'm getting an unhandled exception error on the below: I've changed it without the Me and not a chance, still crashes the app. All I want to do is get...
0
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
I read from a serialport using a worker thread. Because the worker thread t does not loop often, I cannot wait to terminate the worker thread using a boolean in the While condition. So I have a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.