Clipboard says its empty when checked from Asynch Thread?

ryodoan

Well-known member
Joined
Jul 16, 2004
Messages
65
Location
Ohio
Programming Experience
3-5
The program I am working on has a long task to run, at the end of the task it puts some text in the clipboard.

When I run the task straight off, "runTask()" it can grab the text off the clipboard without any problems.

here is the code I am using to grab the text from the clipboard:
VB.NET:
private sub runTask()
...
Dim strTaskResult As String = My.Computer.Clipboard.GetText()
...
end sub

However, its a fairly long task (10-15 seconds, maybe longer) and I dont want the form to become unresponsive. So I decided to use a backgroundworker to launch the task:

VB.NET:
bgwTaskRunner.RunWorkerAsync()

Private Sub bgwTaskRunner_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwTaskRunner.DoWork
        runTask()
    End Sub

However, at the end of the task, when it is supposed to have copied text from the clipboard, I just get an empty string "".

When I use runTask() by itself, grabs text fine from clipboard, when I use runTask from inside the background worker, grab an empty string.

If I manually press ctrl-v in something like notepad, the text is correctly in the clipboard, its just not being retrieved.

Any help would be much appreciated, thanks in advance.

*edit*
This is kinda important, forgot to say this, I am still in the Asynchronous thread when I am trying to get the text from the clipboard, so its not like the Asynchronous thread finished after I tried to grab the text from the clipboard as would happen if they were in different threads.
 
Clipboard object can only be accessed by a single apartment thread. To manage this with multithreading you have to explicitly create a thread and set its apartment state to STA. But why do you need to access clipboard at all? Pass the result of the worker through e.Result and read it in UI safe RunWorkerCompleted event.
 
I need the clipboard because I am running a script that copies text out of a window.

The long description is this. Valve (the game company) makes a program called steam to manage their games and other developers games. They recently implemented a community system with groups. I am part of a Team Fortress 2 mappers group and people frequently use the group chat to ask / answer questions. When Valve created the group chat they left out a "Log Chats" option and group chat logs would be a really nice resources for finding old information.

So, because I am bored and because I have a few spare computers lieing around, I wrote a simple VB.net / AutoIt3 hybrid chat logger program. The AutoIt3 script copies the text out of the group chat window, the VB.net program grabs the text out of the clipboard, parses it into HTML, and outputs the result.

I am just doing a re-write of the logger to make it more reliable, efficient, and more visually appealing. I have gotten most of the kinks worked out and just put the new version live: http://caffeinesystems.ath.cx/Tf2mappers/

The AutoIt3 script takes about 10-15 seconds to run, and at the moment my program locks up while it waits for the script to exit, it would be nice, but not altogether necessary to have the script run in another thread. However, I do not know much about multi-threading and apartment states.
 
However, I do not know much about multi-threading and apartment states.
It's the simplest of matters:
VB.NET:
Dim t As New Threading.Thread(AddressOf worker)
t.IsBackground = True
t.SetApartmentState(Threading.ApartmentState.STA)
t.Start()
VB.NET:
Sub worker()
    'here in STA thread you can use Clipboard
End Sub
 
Back
Top