SHEHAB
SHEHAB

Reputation: 13

VB.NET ERROR WHEN USING My.Computer.Clipboard.SetText Into thread

i got this error ({"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."}) when i try to use My.Computer.Clipboard.SetText Into thread

this is my code

Imports System.Threading
Public Class Form1
    Dim startth As Thread
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        startth = New Thread(AddressOf Me.MYTHREAD)
        startth.Start()
    End Sub
    Private Sub MYTHREAD()
        AppActivate("Untitled - Notepad")
        My.Computer.Clipboard.SetText(TextBox1.Text)
        SendKeys.SendWait("^(v) {Enter}")
        My.Computer.Clipboard.SetText(TextBox2.Text)
        SendKeys.SendWait("^(v) {Enter}")
    End Sub
End Class

Upvotes: 1

Views: 1537

Answers (1)

cremor
cremor

Reputation: 6865

The exception already tells you what you have to do. Set the apartment state of the thread you create:

startth = New Thread(AddressOf Me.MYTHREAD)
startth.SetApartmentState(ApartmentState.STA)
startth.Start()

Upvotes: 2

Related Questions