471,337 Members | 1,256 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Clipboard in another thread of non-Form class

class Engine
{
Thread Worker;
public event ... EngineMessage;
public void Start()
{
Worker=new Thread(new ThreadStart(Run));
Worker.Start();
}
private Run()
{
Clipboard.GetText(); <--Exception.
}
}

Since this class 'Engine' is not a Windows Form, I can't use
InvokeRequired. Non-Form class has no way to access the clipboard in
another thread? Or may I have to use WinAPI to directly access the
clipboard bypassing .NET BCL?

Thank you.

Aug 11 '07 #1
5 4008
Sin Jeong-hun wrote:
[...]
Since this class 'Engine' is not a Windows Form, I can't use
InvokeRequired. Non-Form class has no way to access the clipboard in
another thread? Or may I have to use WinAPI to directly access the
clipboard bypassing .NET BCL?
I think it's a little odd for a non-GUI application to be accessing the
clipboard, but especially if all you're doing is reading from it, that's
probably not going to confuse the user.

But as far as the "not a Windows Form" goes, I'm not aware of any
limitation in the Clipboard class requiring it to be used from a
specific thread. Could you be more specific about what the issue is
here? Why do you say "non-Form class has no way to access the clipboard
in another thread"? What happens when you try to do that?

Pete
Aug 11 '07 #2

On Aug 11, 7:39 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
Since this class 'Engine' is not a Windows Form, [...]

[...] as far as the "not a Windows Form" goes, I'm not aware of any
limitation in the Clipboard class requiring it to be used from a
specific thread.
Still, "The Clipboard class can only be used in threads set to single
thread apartment (STA) mode. To use this class, ensure that your Main
method is marked with the STAThreadAttribute attribute" according to
the System.Windows.Forms.Clipboard docs. Looking at the Clipboard
class, it uses old OLE code from hell, so I can't see it working in
non-form .NET environments.

There's more on the subject here: http://msdn.microsoft.com/msdnmag/is...9/CuttingEdge/

Aug 11 '07 #3
UL-Tomten wrote:
Still, "The Clipboard class can only be used in threads set to single
thread apartment (STA) mode. To use this class, ensure that your Main
method is marked with the STAThreadAttribute attribute" according to
the System.Windows.Forms.Clipboard docs.
That's System.Windows.Forms.Clipboard. The OP doesn't specify, but
there is also a System.Windows.Clipboard, which does not have the same
limitation. It is new to .NET 3.0 however.

Even using the Forms.Clipboard, I don't see that being
STAThreadAttribute is necessarily a problem. I would be more concerned
about some of the other limitations of that class. Even if I had a
Form-based application, if using the new System.Windows.Clipboard class
was an option, I'd try that first.
Looking at the Clipboard
class, it uses old OLE code from hell, so I can't see it working in
non-form .NET environments.
You never know until you try. But the OP didn't say that's the class
he's trying to use. For that matter, the OP wasn't very specific about
what issue he's actually asking about. I guess we'll have to wait for a
clarification.

Pete
Aug 12 '07 #4
On Aug 12, 12:29 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com>
wrote:
UL-Tomten wrote:
Still, "The Clipboard class can only be used in threads set to single
thread apartment (STA) mode. To use this class, ensure that your Main
method is marked with the STAThreadAttribute attribute" according to
the System.Windows.Forms.Clipboard docs.

That's System.Windows.Forms.Clipboard. The OP doesn't specify, but
there is also a System.Windows.Clipboard, which does not have the same
limitation. It is new to .NET 3.0 however.

Even using the Forms.Clipboard, I don't see that being
STAThreadAttribute is necessarily a problem. I would be more concerned
about some of the other limitations of that class. Even if I had a
Form-based application, if using the new System.Windows.Clipboard class
was an option, I'd try that first.
Looking at the Clipboard
class, it uses old OLE code from hell, so I can't see it working in
non-form .NET environments.

You never know until you try. But the OP didn't say that's the class
he's trying to use. For that matter, the OP wasn't very specific about
what issue he's actually asking about. I guess we'll have to wait for a
clarification.

Pete
Since that application is a .NET 2.0 application, I cannot use the new
System.Windows.Clipboard, and the problem I mentioned is exactly the
same thing. It said make sure the Main thead is marked as STAThread,
and it was.

Anyways, I worked around this problem by sending messages to the Main
Window so that let Main Window do the clipboard work for the non-Form
class.

Thank you for your replies.

Aug 12 '07 #5

On Aug 12, 5:58 am, Sin Jeong-hun <typing...@gmail.comwrote:
Anyways, I worked around this problem by sending messages to the Main
Window so that let Main Window do the clipboard work for the non-Form
class.
Congratulations! This is the founding principle behind Windows. Using
that pattern, you're building on 30 years of proven quality.

Aug 12 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Christopher | last post: by
4 posts views Thread by Wayne Wengert | last post: by
2 posts views Thread by Roy Gunnarsson | last post: by
2 posts views Thread by yxq | last post: by
7 posts views Thread by Dave Booker | last post: by
9 posts views Thread by Alan T | last post: by
2 posts views Thread by Fade | last post: by
23 posts views Thread by Over | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.