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,618 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

13 Experts available now in Live!

Get 1:1 Help Now