473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Major issue with STAThreading and Clipboard object in vb.net

I have converted a VB6 application to VB.NET. The old application
made extensive use of the Clipboard for copying an Image Name so that
it could be pasted into the image capture app when the user scans the
image. My applications run as compiled assemblies from our Intranet
server and I have setup a Main sub routine in the application with the
following line:

<STAThread()> Public Sub Main()

Note: There is no timer in this application.

Now the main issue is that I still get a threading error when using
the app.
This has occurred in other applications I have created using the same
method in order to have access to the clipboard object. The error
occurrs occasionally, not every time. The consistency is frustrating.

I need a workaround for this method. Is it possible to highlight the
value and send a Ctrl-C to the form or control to put the value on the
clipboard without using the Clipboard.SetDataObject?

Anyone have any ideas? I would be extremely grateful!!!
Sincerely,

Tim Frawley
Nov 20 '05 #1
9 5921
Hi Tim,

Thanks for posting in the community.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that when you call the SetDataObject in
an STA console application, you got threading error.
You are working on an STA console application, there is no other thread in
your application.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

What is the exact error message you got?
Is the object you set to the clipboard a string or an image?

Here is my test code , this is an console application.
You may try to call SetDataObject override function

Places data on the system clipboard and specifies whether the data should
remain on the clipboard after the application exits.
[Visual Basic] Overloads Public Shared Sub SetDataObject(Object, Boolean)

Imports System.Windows.Forms
Imports System.Drawing
Module Module1
<STAThread()> _
Public Sub Main()
Clipboard.SetDataObject("Test String", True)
'Dim img As New Bitmap("c:\Waterlilies.jpg")
'Clipboard.SetDataObject(img, True)
End Sub
End Module

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #2


Hi Peter,

No, this is not a console application. I have compiled it as a dll
which is loaded via a 'stub' application from our Intranet server.
Esentially it is a windows application distributed via the web.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #3
Hi guys,

This is essentially a smart client application.
Running the application directly will suceed but fail if you deploy. This is
because the exe gets downloaded and run under ieexec process.

This process is running as MTA because there could be many smart client
applications running or other processes under the ieexec. It is also because
the default for .net applications is to be MTA as .net assembles can support
multithreading.

The under lying clipboard processes use COM and the COM object must be
executed in an STA thread.

A normally executed application has an STA thread for its GUI and this is
why a normal application can use the clipboard operations.

OK all interesting but what can you do.
The way I do it is to create a member variable that is a thread.

Where you want to use teh clipboard create a new thread in this variable and
set it to STA. You need to do this because you can not change your current
thread to STA from MTA because it is too late teh thread is already
executing in MTA.

Then do your clipbaord op on that thread.

Note on the form dispose you must check if this thread is active and dispose
it appropriately.

You must also check if it is active before doing another clipboard
operation.

Here is some sample code in C# I posted to the C# group on this

Please note this is a summary version of my code. Error handling and cleanup
in the main dispose etc are required.

I use two functions one to invoke the thread
================================================== ==========
private void CopyMenu_Click(object sender, System.EventArgs e)

{

if (th != null)

if (th.IsAlive)

th.Abort();

th = new Thread(new ThreadStart(CopySTA));

th.ApartmentState = ApartmentState.STA ;

th.Start();

}

================================================== ==========
And the other is called by the thread

[STAThread]

private void CopySTA()

{

Clipboard.SetDataObject(this.textBox1.Text);

Thread.CurrentThread.Join();

}

================================================== ==========

Hope this helps
Daryl
"Tim Frawley" <ti*********@fishgame.state.ak.us> wrote in message
news:OS**************@TK2MSFTNGP10.phx.gbl...


Hi Peter,

No, this is not a console application. I have compiled it as a dll
which is loaded via a 'stub' application from our Intranet server.
Esentially it is a windows application distributed via the web.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #4
Hi Tim,

Thanks for your quickly reply!

I have made a test, this is my test result.
1. I make a stub application as below, I will test to run the application
by using the URL
http://website/TestStub.exe

[TestStub.exe]
Imports System.Reflection
Imports System.Windows.Forms
Module Module1
<STAThread()> _
Sub Main()
Dim SampleAssembly As [Assembly]
SampleAssembly =
[Assembly].LoadFrom("http://website/TesSmartClient.dll")
' Obtain a reference to a method known to exist in assembly.
Dim tp As Type = SampleAssembly.GetTypes()(0)
Dim o As Object =
SampleAssembly.CreateInstance("TesSmartClient.Form 1")
o.GetType().InvokeMember("Show", BindingFlags.InvokeMethod,
Nothing, o, New Object() {})
Application.Run(o)
End Sub
End Module

2. I make windows form dll application to access the local machine
clipboard.
[TesSmartClient.dll]

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox(Threading.Thread.CurrentThread.ApartmentSta te.ToString())
'I get the thread ApartmentState STA
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim str As String = "Hello World"
Clipboard.SetDataObject(str, True)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
' Declares an IDataObject to hold the data returned from the
clipboard.
' Retrieves the data from the clipboard.
Dim iData As IDataObject = Clipboard.GetDataObject()

' Determines whether the data is in a format you can use.
If iData.GetDataPresent(DataFormats.Text) Then
' Yes it is, so display it in a text box.
MsgBox(CType(iData.GetData(DataFormats.Text), String))
Else
' No it is not.
MsgBox("Could not retrieve data off the clipboard.")
End If
End Sub

Is this your senario?
If yes, please test my code to see if the problem persists.

If no can you describe your senario more detailed and post the exactly
error messge your got, because I can not reproduce the problem on my side

My test environment is windows XP+SP1 VS.NET2003 + NET framework 1.1.

To isolate the problem, I suggest you run the application as an
administrator, and run the smartclient(run in IE browser) from the
localhost to make a test.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #5


I notice that you are STAThreading your stub which is not something that
I was doing. I will test that out.

My method for loading the assembly is a little different, primarily I do
not have all the arguments that you have in your test. Here is a sample
of what I am doing.

Select Case strApp
Case "SI"
strAssembly = "ScaleInput.dll"
strAssemblyForm = "ScaleInput.frmInput"
Case "SM"
strAssembly = "ScaleImport.dll"
strAssemblyForm = "ScaleImport.frmImport"
Case "SG"
strAssembly = "ScaleImage.dll"
strAssemblyForm = "ScaleImage.frmScaleImage"
End Select
Dim formAsm As [Assembly]
Dim formtype As Type
Dim FormObj As Object
Dim Form1 As Form

formAsm = [Assembly].LoadFrom(gstrAssemblyLocation & strAssembly)
formtype = formAsm.GetType(strAssemblyForm)
FormObj = Activator.CreateInstance(formtype)
Form1 = CType(FormObj, Form)
Form1.ShowDialog()
I am using the stub to load multiple possible applications via command
line arguments passed via the hyperlink. This way I can tell the stub
which app to load, weather to load in test mode, etc. I use property
assignments in the assembly to fill the values. In one of our apps we
set the connection string to the database in this manner.

In any case, your methods are the same but the number of arguments is
different. Mine seems a little streamlined but I do not know what all
the arguments are for. I only know that this works. :)

I will play around with your test and see what comes of it.

Thanks for helping me out Peter!

Tim

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #6
Hi Tim,

Based on my test, by deafult the thread apartment of [TestStub.exe] will be
<STAThread()>. And if we specify it to MTAThread we will get error when
access to the clipboard.

I think you code seems to be similar with my testcode. I look forward to
hearing from you after you have a test with my code.
Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #7
Currently my app is STAThread in the Sub Main of the assembly that uses
the Clipboard.SetDataOjbect method as shown here:
Module modBase

<STAThread()> Public Sub Main()
Try
Application.Run(New frmScaleImage)
Catch ex As Exception
Dim strError As String
If gblnTEST_MODE Then
strError = "TESTMODE: frmInput.Main()"
Else
strError = "frmInput.Main()"
End If
WriteToEventLog(strError, ex.ToString)
MsgBox("An unexpected application error has occurred." &
vbCrLf & vbCrLf & _
"Please leave this message on the screen and
call a programmer." & _
strError & vbCrLf & vbCrLf & ex.ToString)
End Try
End Sub

My Catch didn't 'catch' this error either.

The app worked fine today for a while but just now blew up. This is why
I am saying that the consistency if frustrating.

This is the error I got today when using the app:

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.Threading.ThreadStateException: The current thread must set to
Single Thread Apartment (STA) mode before OLE calls can be made. Ensure
that your Main function has STAThreadAttribute marked on it.
at System.Windows.Forms.Clipboard.SetDataObject(Objec t data, Boolean
copy)
at System.Windows.Forms.Clipboard.SetDataObject(Objec t data)
at ScaleImage.frmScaleImage.tdbgData_Click(Object sender, EventArgs
e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at C1.Win.C1TrueDBGrid.BaseGrid.Frame.OnClick(EventAr gs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at C1.Win.C1TrueDBGrid.C1TrueDBGrid.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
System.Drawing
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50
a3a/system.drawing.dll
----------------------------------------
System
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/syst
em.dll
----------------------------------------
RegexAssembly8_0
Assembly Version: 0.0.0.0
Win32 Version: n/a
CodeBase:
----------------------------------------
IEExecRemote
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/ieexecremote/1.0.5000.0__b03f5f7f11d50a3
a/ieexecremote.dll
----------------------------------------
Stub
Assembly Version: 1.0.1525.15805
Win32 Version: n/a
CodeBase: http://intranet.taglab.org/TMR/ScaleApps/Stub.EXE
----------------------------------------
System.Windows.Forms
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c5
61934e089/system.windows.forms.dll
----------------------------------------
System.Xml
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/
system.xml.dll
----------------------------------------
Microsoft.VisualBasic
Assembly Version: 7.0.5000.0
Win32 Version: 7.10.3052.4
CodeBase:
file:///c:/windows/assembly/gac/microsoft.visualbasic/7.0.5000.0__b03f5f
7f11d50a3a/microsoft.visualbasic.dll
----------------------------------------
ScaleImage
Assembly Version: 1.1.1525.25632
Win32 Version: n/a
CodeBase: http://intranet.taglab.org/TMR/ScaleApps/ScaleImage.DLL
----------------------------------------
C1.Win.C1TrueDBGrid
Assembly Version: 1.2.20033.30829
Win32 Version: 1.2.20034.31024
CodeBase:
file:///c:/windows/assembly/gac/c1.win.c1truedbgrid/1.2.20033.30829__75a
e3fb0e2b1e0da/c1.win.c1truedbgrid.dll
----------------------------------------
System.Data
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/system.data/1.0.5000.0__b77a5c561934e089
/system.data.dll
----------------------------------------
C1.Common
Assembly Version: 1.0.20031.116
Win32 Version: 1.0.20031.117
CodeBase:
file:///c:/windows/assembly/gac/c1.common/1.0.20031.116__e272bb32d11b194
8/c1.common.dll
----------------------------------------
Accessibility
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase:
file:///c:/windows/assembly/gac/accessibility/1.0.5000.0__b03f5f7f11d50a
3a/accessibility.dll
----------------------------------------

************** JIT Debugging **************
To enable just in time (JIT) debugging, the config file for this
application or machine (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the machine
rather than being handled by this dialog.


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #8


I just implemented <STAThread()> on the Stub as well and still received
the error.

Uhg....


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #9
Hi Tim,

Thanks for posting in the community.

Yes, when the application.run is in the try---catch block, the catch
statement will not catch the exception.
Here is a link may help you.
http://groups.google.com/groups?q=ap...g-shen+yu%22&h
l=zh-CN&lr=&ie=UTF-8&oe=UTF-8&selm=TxRqBsd2DHA.3532%40cpmsftngxa07.phx.gbl&r
num=1

To further troubleshoot the problem, I suggest you try my test code first
to see if the problem persists. This will help us to know if the problem is
about the coding of your dll, or the environment of your machine.

I will appreciate your efforts that you can make a test with the demo I
post previously and let me know the result.

If my demo works for you, please add code based on my demo to see what
cause the problem. From the error message, I think the problem may be
caused by when you access the clipboard, the thread apartment will changed
to MTA, which cause the problem. So please add event log before each method
invoke to trace when the ThreadApartmentstate was changed to MTA.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: LG | last post by:
Just have a question with regards to the clipboard, and how to read what other applications (Adobe InDesignCS) place in the clipboard. I am currently in the process of creating a booklet from a...
5
by: TC | last post by:
Hello, Here is what I'm trying to do: -- Make sure both MS Excel and MS Word are running -- Create an Excel chart -- Save the Excel file -- Copy the Excel chart onto the clipboard using Ctrl...
2
by: Sunny | last post by:
Hi I am using Data Objects in my Applications and need to clear the ClipBoard or Memory Buffer after some intervals How do i clear the contents of the System.ClipBoard With Regard Sunny
1
by: keith | last post by:
I read a book, Programming Microsoft WINDOWS with C# by Charles Petzold, which shows ho to set or get string and image to and from Clipboard. It says even a button object can be set or go to and...
3
by: keith | last post by:
I can use following codes to set and get object (serializable class) from Clipboar Clipboard.SetDataObject(obj) then DataObject data=new DataObject()...
4
by: Wayne Wengert | last post by:
I have an aspx page on which I am trying to copy the contents of a textbox to the client clipboard when the users clicks a button. The button code is as follows:...
2
by: Shayne H | last post by:
I am trying to write a method to copy some text to the clipboard, and leave a copy of it there after the application has exited. Sub CopyToClipboard(ByVal value As Object) Dim data As New...
5
by: DraguVaso | last post by:
Hi, I'm looking for a way to Copy and Paste Files to the clipboard. I found a lot of articles to copy pieces of text and bitmaps etc, but nog whole files. Whay I need is like you have in...
0
by: =?Utf-8?B?UGV0ZQ==?= | last post by:
This is a duplicate of a posting on Officedev so apologies to anyone who's read this already. I had an Office add-in built in VB.Net 2003 with an Excel and a Word add-in class that transferred...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.