Solved

ex = {"Cross-thread operation not valid: Control 'dataGridView1' accessed from a thread other than the thread it was created on."}

Posted on 2006-07-17
5
1,619 Views
Last Modified: 2008-01-09
this should be a good one!

I need some help, i basically have a winform with a gridview on it. What happens is that the user selects a folder with some images in it and then the images load into a datagridview. the issue is that when they take high res pics and there are more than a couple the form locks while the pictures are loaded. I want to try keep their blood pressure down as they dont have high spec machines, so i wanted to implement a multi thread scenario. i would like a child thread to load the images into the grid and update a progress bar while they still have a responsive gui.... i am fine with loading the pics and progress bar ... its the child thread accessing the controls in the main application thread.

there is actually a lot of info on this on the web error message, I am am sure that i will have to spend a few hours reading this up. I have vs 2005 (net 2.0) but have mainly been working with asp.net, i have a solid .net 1.1 book that i might have to dust the cob webs off to see if there are some examples child threads accessing cross threads controls (resources).

I need advice, i have already read about this sort of thing but not realy sure what is going on....                
if (Control.InvokeRequired)
                {
                      Control.Invoke(new MethodInvoker(Control.Hide()));
                }

if there is some code examples then much appreciated!! I have read some msdn example which are excellent but they go a bit over my head...
0
Comment
Question by:deanvanrooyen
  • 4
5 Comments
 
LVL 12

Author Comment

by:deanvanrooyen
ID: 17127503
ps. thanks
0
 
LVL 12

Author Comment

by:deanvanrooyen
ID: 17127518
I notice there is a background worker control -  how is this different to say creating my own thread -

                Thread myThread = new Thread(new ThreadStart(MyFunction));
                myThread.Priority = ThreadPriority.AboveNormal;
                if (!myThread.IsAlive)
                    myThread.Start();
0
 
LVL 12

Author Comment

by:deanvanrooyen
ID: 17127637
well well,

I get the same error cross thread usin the backgroundworker ... I took the example straight out msdn, and it failing because what I do it I put a progress bar into a toolstrip in the child thread / background worker
like so

                //setup progress
                ProgressBar progressBar1 = new ProgressBar();
                progressBar1.Height = 10;
                progressBar1.Width = 100;
                ToolStripControlHost host = new ToolStripControlHost(progressBar1, "Progress");
                toolStrip1.Items.Insert(7, host);

Cross-thread operation not valid: Control toolStrip1 accessed from a thread other than the thread it was created on."}
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 17127783
0
 
LVL 12

Author Comment

by:deanvanrooyen
ID: 17127998
I sorted it actually this is a very good eg in ms help
//ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxmclictl/html/138f38b6-1099-4fd5-910c-390b41cbad35.htm


you gets the points for quick assist thanks


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now