Major issue with STAThreading and Clipboard object in vb.net

T

Tim Frawley

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
 
P

Peter Huang

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.
 
T

Tim Frawley

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.
 
D

Daryl

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
 
P

Peter Huang

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.Form1")
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.ApartmentState.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.
 
T

Tim Frawley

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
 
P

Peter Huang

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.
 
T

Tim Frawley

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(Object data, Boolean
copy)
at System.Windows.Forms.Clipboard.SetDataObject(Object 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(EventArgs 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(Message& 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.
 
T

Tim Frawley

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

Uhg....
 
P

Peter Huang

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=application+try+catch+"ying-shen+yu"&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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top