Windows Service & Clipboard

  • Thread starter Thread starter Humam
  • Start date Start date
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?
 
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.)
 
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.
 
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.
 
Back
Top