PC Review


Reply
Thread Tools Rate Thread

Clipboard, no-touch deployment, STAThread error

 
 
Erik
Guest
Posts: n/a
 
      26th Jan 2004
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
 
Reply With Quote
 
 
 
 
Daryl
Guest
Posts: n/a
 
      26th Jan 2004
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

"Erik" <(E-Mail Removed)> wrote in message
news:41b901c3e42c$d6dff000$(E-Mail Removed)..
> 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



 
Reply With Quote
 
Erik
Guest
Posts: n/a
 
      26th Jan 2004
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

>-----Original Message-----
>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
>
>"Erik" <(E-Mail Removed)> wrote in

message
>news:41b901c3e42c$d6dff000$(E-Mail Removed)..
>> 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

>
>
>.
>

 
Reply With Quote
 
Daryl
Guest
Posts: n/a
 
      27th Jan 2004
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

"Erik" <(E-Mail Removed)> wrote in message
news:49f401c3e461$6f317a20$(E-Mail Removed)...
> 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
>
> >-----Original Message-----
> >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
> >
> >"Erik" <(E-Mail Removed)> wrote in

> message
> >news:41b901c3e42c$d6dff000$(E-Mail Removed)..
> >> 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

> >
> >
> >.
> >



 
Reply With Quote
 
Erik
Guest
Posts: n/a
 
      27th Jan 2004
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
>
> "Erik" <(E-Mail Removed)> wrote

in message
> news:49f401c3e461$6f317a20$(E-Mail Removed)...
> > 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
> >>>-----Original Message-----
> >>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
> >>>>"Erik" <(E-Mail Removed)>

wrote in
> > message
> >>news:41b901c3e42c$d6dff000$(E-Mail Removed)..
> >>> 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
> >>>>>>.
> >>

>.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# - [STAThread] Still throws expection with Clipboard Sefner Microsoft C# .NET 1 15th Jul 2006 08:03 AM
Clipboard still throws exception with [STAThread] renfes@gmail.com Microsoft C# .NET 0 14th Jul 2006 10:04 PM
CGI Error w/No Touch Deployment Application Anil Kripalani Microsoft ASP .NET 3 15th Apr 2005 04:57 PM
RE: CGI timeout error with No-touch deployment =?Utf-8?B?RGF2ZSBT?= Microsoft Dot NET Framework Forms 0 9th Sep 2004 08:39 PM
No Touch Deployment Error Question Paul Hetherington Microsoft Dot NET Framework Forms 0 7th Jan 2004 07:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:00 PM.