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
- Public Class Form1
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- trd = New Threading.Thread(AddressOf trdTask)
- trd.IsBackground = True
- trd.Start()
- End Sub
- Private trd As Threading.Thread
- Private Sub trdTask()
- Dim file As OpenFileDialog = New OpenFileDialog
- file.ShowDialog()
- End Sub
- End Class
Thanks,
Wagz