STAThreadAttribute Exception!

Master Zero

Well-known member
Joined
Sep 10, 2005
Messages
51
Programming Experience
Beginner
STAThreadAttribute Exception!

I really do not understand why I am getting this error. This is the exception that was thrown: “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.”

The exception happened at the follow code, “Select Case .ShowDialog(). Here is the complete thread:
VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] OnRead([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] AR [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IAsyncResult)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Data [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Message [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DataArray() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2]Data = TCPC.GetStream.EndRead(AR)[/SIZE]
[SIZE=2]
Message = Encoding.ASCII.GetString(BytesToRead, 0, Data)
 
DataArray = Message.Split([/SIZE][SIZE=2][COLOR=#800000]"|"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] FolderBrowserDialog1
[/SIZE][SIZE=2][COLOR=#0000ff]Select[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] .ShowDialog()
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] Windows.Forms.DialogResult.Cancel
[/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] Windows.Forms.DialogResult.OK
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] File.Exists(.SelectedPath & DataArray(1)) = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Writer [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] StreamWriter(TCPC.GetStream)[/SIZE]
[SIZE=2]
Writer.Write([/SIZE][SIZE=2][COLOR=#800000]"RESUME|NO"[/COLOR][/SIZE][SIZE=2])
Writer.Flush()
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]strFile = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FileInfo(.SelectedPath & DataArray(1))[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Writer [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] StreamWriter(TCPC.GetStream)[/SIZE]
[SIZE=2]
Writer.Write([/SIZE][SIZE=2][COLOR=#800000]"RESUME|YES|"[/COLOR][/SIZE][SIZE=2] & strFile.Length + 1)
Writer.Flush()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Select
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

Thanks in advances!
 
That makes sense, but why? Does anyone know the reason why I can not call it from within the same method? Oh, and I started this project in 2005.

Thanks again.
 
Last edited:
Ok, I have added the code to its own thread and I am still getting that exception. I even commented out the “with” and “select case” statements to keep it simple, but it is no good. Have a look.
VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] OnRead([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] AR [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IAsyncResult)[/SIZE]
[SIZE=2]
Data = TCPC.GetStream.EndRead(AR)
Message = Encoding.ASCII.GetString(BytesToRead, 0, Data)
 
DataArray = Message.Split([/SIZE][SIZE=2][COLOR=#800000]"|"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Process [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Threading.Thread([/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] ProcessData)
Process.Start()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ProcessData()[/SIZE]
[SIZE=2]
FolderBrowserDialog1.ShowDialog()
 
[/SIZE][SIZE=2][COLOR=#008000]'With FolderBrowserDialog1
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Select Case .ShowDialog()
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Case Windows.Forms.DialogResult.Cancel
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Case Windows.Forms.DialogResult.OK
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' If File.Exists(.SelectedPath & DataArray(1)) = False Then
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Dim Writer As New StreamWriter(TCPC.GetStream)
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Writer.Write("RESUME|NO")
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Writer.Flush()
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Else
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' strFile = New FileInfo(.SelectedPath & DataArray(1))
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Dim Writer As New StreamWriter(TCPC.GetStream)
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Writer.Write("RESUME|YES|" & strFile.Length + 1)
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Writer.Flush()
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' End If
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' End Select
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'End With
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

Any help will be greatly appreciated!
 
Back
Top