Windows Service & Clipboard

H

Humam

I am developing a windows service that read data from a device attached to a
PC via serial port, then service should put the data in the clipboard.
I am writing this code:

Clipboard.Clear();
Clipboard.SetText("Some Text");

but for some reason, no data is sent to the clipboard!
Is it because Windows Service run on a seperate thread?
Can any one help?
 
J

Jon Skeet [C# MVP]

Humam said:
I am developing a windows service that read data from a device attached to a
PC via serial port, then service should put the data in the clipboard.
I am writing this code:

Clipboard.Clear();
Clipboard.SetText("Some Text");

but for some reason, no data is sent to the clipboard!
Is it because Windows Service run on a seperate thread?
Can any one help?

Have you allowed the service to interact with the desktop? I believe
the clipboard is inherently tied to the desktop, and services aren't
normally meant to interact with the UI at all.

(I have to say, it does seem a very odd thing to do.)
 
M

Mr. Arnold

Humam said:
I am developing a windows service that read data from a device attached to
a
PC via serial port, then service should put the data in the clipboard.
I am writing this code:

Clipboard.Clear();
Clipboard.SetText("Some Text");

but for some reason, no data is sent to the clipboard!
Is it because Windows Service run on a seperate thread?
Can any one help?

I would think because it's not a Windows Desktop solution, forms based, that
you might have a problem doing that.
 
W

Willy Denoyette [MVP]

Only STA threads can access the clipboard, Windows Services threads enter
the MTA by default. Even if you run your clipboard stuff on a new thread
initialized to enter an STA, you will encounter following issues:
1) STA threads must create a Window and pump it's message queue.
2) The clipboard is a shared memory section, mapped in all the processes
memory space running in the same WindowsStation\Desktop pair sharing the
same Session. Windows Services and Interactive applications run in distinct
pairs (unless you set "Access Interactive Desktop", which is a bad idea),
so, by default, they don't share the clipboard.
On Vista ( and on systems where FUS is enabled/used) it's no longer
possible to use the clipboard to pass data across the session boundaries.
Services and user applications do no longer share the same session on Vista.


Willy.
 

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