Clipboard, no-touch deployment, STAThread error

  • Thread starter Thread starter Erik
  • Start date Start date
E

Erik

I have an app being deployed via no-touch deployment.
Within the app I do some simple clipboard operations
using the .Net clipboard object. The app runs fine after
normal deployment, but with no-touch, I get the following
error when trying to set clipboard data:

"The current thread must be set to Single Thread
Apartment (STA) mode before OLE calls can be made.
Ensure you Main function has STAThread Attribute marked
on it."

My app is set to STA (as all WinForms apps are by
default). Anyone know another way around this?

Thanks,
Erik
 
Hi Erik,

This is a common problem that I have come across. When you deploy using no
touch or smart client your application is running under the 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.

Hope this helps

Regards
Daryl
 
Thanks for the info and advice.

I tried the thread trick earlier and it still failed.
Maybe I did something wrong, so I'll try again.

If you have any sample code to post, I would really
appreciate it.

Thanks again,
Erik
 
Hi Erik,

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();

}

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

Regards
Daryl
 
Thanks Daryl for posting the sample code. It worked like
a charm this time around...
Erik
----- Daryl wrote: -----

Hi Erik,

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();

}

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

Regards
Daryl

Thanks for the info and advice. failed.
Maybe I did something wrong, so I'll try again. really
appreciate it. across. When
you deploy using no under
the ieexec process. could be
many smart client as .net
assembles can support the COM
object must be thread for
its GUI and this is that is a
thread. new thread
in this variable and can not
change your current thread
is already thread
is active and dispose doing another
clipboard wrote in
message fine
after
.
 
Back
Top