473,396 Members | 2,089 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 4146
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: LG | last post by:
Just have a question with regards to the clipboard, and how to read what other applications (Adobe InDesignCS) place in the clipboard. I am currently in the process of creating a booklet from a...
0
by: Christopher | last post by:
Hello, First, I appologize for the length but I want to get all the facts down so as not to waste any time :) I'm trying to allow the user of my C# app to copy a DataGrid full of double values...
4
by: Wayne Wengert | last post by:
I have an aspx page on which I am trying to copy the contents of a textbox to the client clipboard when the users clicks a button. The button code is as follows:...
2
by: Roy Gunnarsson | last post by:
Hello all, I have a VERY simple windows application that decrypts data files we receive from a partner. The decryption is handled by a COM component our partner provided us with (thus we have no...
2
by: yxq | last post by:
Hello I want to get Windows clipboard data size, seem to use the function "GetClipboardDataSize". Could anyone please tell how to do using vb.net? Thanks
5
by: DraguVaso | last post by:
Hi, I'm looking for a way to Copy and Paste Files to the clipboard. I found a lot of articles to copy pieces of text and bitmaps etc, but nog whole files. Whay I need is like you have in...
7
by: Dave Booker | last post by:
My Clipboard is getting into some very confused state where I can't even clear it. No applications can access the clipboard in this state, and if I run Clipboard.Clear() I get: ...
9
by: Alan T | last post by:
Any source code example I can use to save the clipboard content to an object, then I will do a copy and paste, finally put back the 'saved' clipboard content back to clipboard ?
2
by: Fade | last post by:
In my app, I use clipboard to paste some html text into ppt. The program runs correctly in VS2005 development server, and IIS 5.1 in WinXP SP2. But it failed on WinServer 2003. The thread blocks...
23
by: Over | last post by:
Hi it's possible with language C, (Mingw or Djgpp) copying the clipboard of WinXp into file.txt? which function must be use. Thanks
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.