gpt4 book ai didi

vb.net - 错误: The current thread must be in Single Thread Apartment

转载 作者:行者123 更新时间:2023-12-03 13:09:07 24 4
gpt4 key购买 nike

每当我尝试打开Openfiledialog时,我都会遇到此错误:

The current thread must be in Single Thread Apartment (STA) mode before OLE calls can be made. Make sure your Main function is marked as STAThreadAttribute. This exception is triggered only if a debugger is attached to the process.



我的代码:
    Dim ofg As New OpenFileDialog
Dim lvp As New ListViewItem

ofg.Multiselect = True
ofg.Filter = "All Files|*.*"
Statue.Text = "Loading..."

If ofg.ShowDialog = Windows.Forms.DialogResult.OK Then
For Each File As String In ofg.SafeFileNames

Dim hInst As IntPtr = Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0))
Dim iIcon As Int32 = 0
Dim hIcon As IntPtr

hIcon = ExtractAssociatedIcon(hInst, File, iIcon)
ico = Icon.FromHandle(hIcon)
icondufile = ico.ToBitmap
Img.Images.Add(icondufile)
Dim C_File As New IO.FileInfo(File)

Dim ItemFile As ListViewItem = New ListViewItem(C_File.Name)
lvp = Files_List.Items.Add(ItemFile)
lvp.SubItems.Add(MD5Checksum(File))
lvp.SubItems.Add(hash_generator("sha1", File))
lvp.SubItems.Add(hash_generator("sha256", File))
lvp.SubItems.Add(GetCRC32(File))
lvp.SubItems.Add(hash_generator("sha384", File))
lvp.SubItems.Add(hash_generator("sha512", File))
lvp.SubItems.Add(C_File.FullName)
lvp.SubItems.Add(C_File.Extension)
lvp.StateImageIndex = lvp.Index

ItemFile = Nothing

Next

Private Sub AddFilesToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AddFilesToolStripMenuItem.Click
trd = New Threading.Thread(AddressOf AddFiles)
trd.IsBackground = True
trd.Start()
Timer_AddFiles.Start()
End Sub

最佳答案

您可以使用Form Invoke方法或将Thread Apartment设置为:

Dim ItemFile As ListViewItem
Private Function Init()
trd = New Threading.Thread(AddressOf AddFiles)
trd.IsBackground = True
trd.SetApartmentState(Threading.ApartmentState.STA) ''You can set MTA or STA
trd.Start(ItemFile)
End Function
Private Sub DoFormUiBased()
'' Add your code here
End Sub
Private Function AddFiles(ByVal Argument As Object)
Me.Invoke(New MethodInvoker(AddressOf DoFormUiBased))
End Function

关于vb.net - 错误: The current thread must be in Single Thread Apartment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43131675/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com