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