Tigraine

Daniel Hoelbling-Inzko talks about programming

Gotchas on your way to the Clipboard

We all know that lovely thing nobody can live without: Clipboard.
Have you ever tried to read the clipboard from your C# code?

Your code may have looked like this:

Clipboard.GetDataObject();

Ok, this code works perfectly fine whenever you try it from a normal Windows-Forms Application Template.
Now, go try accessing the Clipboard from within an Console-Application!

Ok, I know you didn't try, that's why I'm writing this anyway.
Clipboard isn't accessible as long as your Main() isn't looking like this:

[STAThread]
        public static void Main()
        {
            
        }

Why? All the magic lies in the STAThread Attribute!


The STAThread Attribute changes the appartment state of the current thread to be single-threaded. This is necessary for some features especially of Windows Forms (where Clipboard resides in). Why?


Mainly because those features are done through COM-Interop and need to be single-threaded.

Great, my whole application relies on MTAThread, now I'm screwed??


No! Thanks to threading you can always fire up a new thread that is using STA.

[MTAThread]
        public static void Main()
        {
            Thread newThread = new Thread(new ThreadStart(StartingPoint));
            newThread.SetApartmentState(ApartmentState.STA);
            newThread.Start();
        }

public static void StartingPoint() { Clipboard.GetDataObject(); }

Voila, now we have a new Thread that can access the Clipboard without having to change the apartment state of our old thread.
Just remember to set the ApartmentState through the SetApartmentState() function before hitting Start()!

You could also just stick the [MTAThread] attribute ontop of your ThreadStart-Method (but that would be too simple wouldn't it?)

So.. be warned, I had an hour of headaches because of this!

Changes incoming!

As some of you may have noticed, I recently started to use this blog to share some of my views on C# programming topics and wrote those posts in English.

I chose English instead of German because obviously everything I am reading on those topics is kept in English (just browse my Blogroll and you'll see what I mean). Now that I recently realized that buying my technical books from Amazon.com saves me quite a penny over doing so at Amazon.de, I am literally breathing articles in English. I also had to realize that while being (somehow) perfectly fluent in English while talking and reading, my written skills seem to have deteriorated. This is something I really hope to be able to improve through this blog and work.

So, apparently this will be the first change: All future posts will be written in (more or less good) English.

I am currently trying to clean up the site, meaning that I will re-think Categories and Tags to fit.

I am also planning to focus a bit more on software development and .NET programming. So expect more on-topic and less off-topic posts in the future.

This is just one of many changes I currently plan to execute over the next weeks.

Filed under site, site-news, site-news

New RSS Feed!

feedburner

One thing that was on my checklist for some months now was getting my RSS Syndication right.

I was thinking about using Feedburner before, but stuck to the Wordpress default Syndication, believing that the Wordpress built-in Feed could do just as good.

I was obviously wrong and finally corrected this mistake today and did set up my new FeedBurner feed!

By clicking on the new Link in the sidebar you will find the new Feed that will not only syndicate this Blog and it's posts, but also my del.icio.us Links.

For all people who where using my old RSS Feed please update your subscription to the new URL.

My Photography business

Projects

dynamic css for .NET

Archives

more