Clipboard in another thread of non-Form class

S

Sin Jeong-hun

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

Peter Duniho

Sin said:
[...]
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
 
U

UL-Tomten

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/issues/03/09/CuttingEdge/
 
P

Peter Duniho

UL-Tomten said:
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
 
S

Sin Jeong-hun

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.


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

UL-Tomten

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.
 

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