Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old March 23rd, 2008, 01:25 PM
Authorized User
Join Date: Jul 2003
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default cross thread exception

hi all!

I'm trying to create a utility that captures an image of a webpage based on the HTML from the source. I did some diging around and found that Windows Live makes an API with that functionality built in :) But when i try and use it i get the following error.

Uri uri = new Uri("http://www.msn.com");
WindowsLive.Writer.Api.HtmlScreenCapture hsc = new HtmlScreenCapture(uri, 800);

pictureBox1.Image = hsc.CaptureHtml(1000);

System.InvalidOperationException: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.

The line 'pictureBox`' refers to a control i have on my windows form.

When i call the basic

it returns the same error...

any ideas are welcome... Thanks in advance

Old March 23rd, 2008, 06:46 PM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

How is this block of code being called? Is this code being called from some other thread you created or in asynchronous operation?

This is the error you typically see if you spin off your own secondary threads (manually or use a "BackgroundWorker") to perform work that is trying to manipulate controls. The controls are "owned" by the initial/primary thread (this is the UI thread). No other thread can make changes to the controls created on the UI thread. You need to transition calls back to the UI thread to do this. This is usually done by calling the Invoke method on the form class that holds the control you want to change. The Invoke method invokes the handler provided to it on the thread it was created on, thus transitioning the control manipulation call back to the proper thread.


Similar Threads
Thread Thread Starter Forum Replies Last Post
cross thread exception with windows live API YoungLuke C# 2008 aka C# 3.0 1 March 23rd, 2008 01:43 PM
Exception in thread "main" javax.xml.transform.Tra XSLTUser XSLT 1 December 29th, 2007 04:59 AM
cross thread Error angelboy C# 2005 1 July 28th, 2007 04:35 AM
Cross-thread operation not valid Richw71 C# 0 December 13th, 2006 09:17 AM
ERROR "Exception in thread "main" java.lang.NoClas nitinnishi BOOK: Beginning Java 2 3 June 17th, 2006 07:07 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.