Control.Invoke must be used to interact with controls created on a separate thread

by Mark Deraeve 20. July 2008 17:35

I've migrated a windows CE 4.20 application to windows CE 5.0. This is not without effort. Some things have to be rewritten. I will explain the steps I took in order to get rid of the error: "Control.Invoke must be used to interact with controls created on a separate thread.".

The application worked fine in windows CE 4.2. What is does is when a transponder is scanned it send an event to the forms. In this event I wanted to set properties of my controls. The code looked like this:

public void HandleISOTagScannedEvent(string isoTag)
            {
                  bool Found = false;
 
                  try
                  {
                        int Index = 0;
                        IEnumerator Enumer = cboThings.Items.GetEnumerator();
                        while(Enumer.MoveNext())
                        {
                             Thing ThisThing = new Thing(((DataRowView)Enumer.Current).Row);
                             if (ThisThing._rfId == isoTag)
                             {
                                   cboThings.SelectedIndex = Index;
                                   this.SetThing(ThisThing._guid);
                                   Found=true;
                                   break;
                             }
                             Index++;
                        }
 
                        if (!Found)
                        {     
                             FormsManager.ShowMessage("Thing {0} does not exist!", isoTag);
                        }
                  }
                  catch (Exception ex)
                  {
                        TraceExceptionForm.Display("TubeSetupForm", "HandleISOTagScannedEvent", ex);
                  }
            }
 

When I migrated the application to .Net 2.0 and CE 5 and deployed it to the mobile device, everything seem to work. Then I started testing and when I scanned a transponder, the error first occurred. Control.Invoke must be used to interact with controls created on a separate thread. I started to Google around and found the solution on this website by David Kline: http://blogs.msdn.com/davidklinems/archive/2006/03/09/548235.aspx. I had to make a delegate on the form and use this to change the properties. Now the code looks like this:

 

private delegate void UpdateStatusDelegate(int index, Guid guid);
 
private void ChangeThing(int index, Guid guid)
        {
            Cursor.Current = Cursors.WaitCursor;
            if (this.InvokeRequired)
            {
                // we were called on a worker thread
                // marshal the call to the user interface thread
                this.Invoke(new UpdateStatusDelegate(ChangeThing),
                            new object[] { index,guid });
                return;
            }
            
            cboThings.SelectedIndex = index;
            this.SetThing(guid);
            Cursor.Current = Cursors.Default;
        } 
 
 
            public void HandleISOTagScannedEvent(string isoTag)
            {
                  bool Found = false;
 
                  try
                  {
                        int Index = 0;
                        IEnumerator Enumer = cboThings.Items.GetEnumerator();
                        while(Enumer.MoveNext())
                        {
                             Thing ThisThing = new Thing(((DataRowView)Enumer.Current).Row);
                             if (ThisThing._rfId == isoTag)
                             {
                        ChangeThing(Index, ThisThing._guid);
                                   Found=true;
                                   break;
                             }
                             Index++;
                        }
 
                        if (!Found)
                        {     
                             FormsManager.ShowMessage("Thing {0} does not exist!", isoTag);
                        }
                  }
                  catch (Exception ex)
                  {
                        TraceExceptionForm.Display("SetupForm", "HandleISOTagScannedEvent", ex);
                  }
            }
 

Problem solved. I hope this can help people with simular problems.

 Maybe someone can explain why on wondows CE 4.2 this was not giving any problem and in windows CE 5.0 it is? Or could it have something todo with the compact framework?  


Tags:

Windows CE

Comments (25) -

Olga
Olga
2/19/2009 4:26:40 PM #

You help me wery mach.
Thanks!

Reply

lengyelg
lengyelg
5/22/2009 3:15:33 AM #

Thx Smile))

Reply

Mark
Mark
6/3/2009 2:59:26 PM #

Your welcome...

Reply

grants for college
grants for college
12/31/2010 3:47:15 PM #

Very nice post and right to the point. I don't know if this is in fact the best place to ask but do you people have any ideea where to hire some professional writers? Thx Smile

Reply

tv online
tv online
12/31/2010 8:36:29 PM #

I wanna recomand you a nice website to watch your favorites tv channels online: http://www.tv-online-live.com

Reply

free online porn
free online porn
1/1/2011 1:37:30 AM #

I will post a link to this page on my blog.

Reply

Marketing Tools
Marketing Tools
1/1/2011 4:05:41 AM #

Managing your Marketing Campaign.It automatically submits it to Video Sites, Photo Sites, Blogs, Micro Blogs, Bookmarking Sites, RSS Feeds,Press Release Sites, and dozens more.<a href="singledad.de/businesspromoting">Marketing Tools</a>

Reply

tera patrick freeones
tera patrick freeones
1/1/2011 3:50:48 PM #

I have studied a few of the articles on your blog, and I genuinely like your style of writing. I added it to my bookmarks blog site directory and will be coming back in the near future.

Reply

Nida Joerger
Nida Joerger
1/1/2011 11:09:59 PM #

I'd like to post a snippet of this article on my web site I hope the author will allow me! Thanks

Reply

online casino
online casino
1/2/2011 11:15:58 AM #

Would you like to post a guest post on my blog?

Reply

Richard Danz
Richard Danz
1/2/2011 10:52:33 PM #

I'd like to post a snippet of this article on my new homepage I hope you will let me! Thanks

Reply

tori black rocco
tori black rocco
1/3/2011 1:01:41 AM #

Your blog is outrageous! I mean, Ive never been so entertained by anything in my life! Your vids are perfect for this. I mean, how did you manage to find something that matches your style of writing so well? Im really happy I started reading this today. Youve got a follower in me for sure!

Reply

Bryan Sulecki
Bryan Sulecki
1/3/2011 8:03:22 AM #

Great post and very well written. Thanks

Reply

Denny Hilderbrand
Denny Hilderbrand
1/3/2011 11:00:04 AM #

Great post and very well written. Thanks

Reply

free webcam sex
free webcam sex
1/3/2011 12:28:38 PM #

I found so lots of interestingstuff on your website above all its debate. I am going to register your RSS feed. Carry on the excellent work.

Reply

Tamie Gunnells
Tamie Gunnells
1/3/2011 6:42:37 PM #

cheers into d' whole new year keep in submitting awesome content

Reply

fridges
fridges
1/3/2011 6:43:32 PM #

I wan't going to comment as this posts a bit old now, but just wanted to say thanks.

Reply

ITO film Manufacturer
ITO film Manufacturer
1/3/2011 6:57:01 PM #

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post. <a href="http://www.lightinges.com">Emergency Lighting</a>: http://www.lightinges.com

Reply

iptv
iptv
1/4/2011 12:47:54 AM #

The two forms of viewing Internet television are streaming the content directly to a media player or simply downloading the program to the user's computer. With the "TV on Demand" market growing, these on-demand websites or applications are a must have for major television broadcasters. For example, the BBC iPlayer brings in users which stream more than one million videos per week, with one of the BBC's headline shows The Apprentice taking over three percent to five percent of the UK's internet traffic due to people watching the first episode on the BBC iPlayer.

Reply

best link wheel
best link wheel
1/4/2011 2:54:43 AM #

Great write-up, I am a big believer in commenting on blogs to assist the weblog writers know that they’ve added something worthwhile to the world huge internet! (supply roblox-cheats.com). Anyway, in my language, there usually are not a lot good supply like this.   

Reply

diet green tea
diet green tea
1/4/2011 4:04:51 AM #

yes me have seen somewhere that the fact to you journal is true

Reply

Backlinks
Backlinks
1/4/2011 5:19:02 AM #

Neat blog layout! Very easy on the eyes.. i like the colors you picked out

Reply

countertop convection ovens
countertop convection ovens
1/4/2011 5:57:06 AM #

Totally new to me; I didn’t know about this until I searched here through Yahoo! Good job.

Reply

Ellie Montejo
Ellie Montejo
1/14/2011 1:33:32 PM #

Hey there, simply wanted to say that I really like your type of writing, in addition to that I hope you continue on writing how you do! www.australianarticlefinder.com/.../...uccess.html

Reply

chanel 2.55
chanel 2.55
1/15/2011 10:48:20 AM #

Straight to the level and properly written, thanks much for that info

Reply

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


About Mark Deraeve

I started as a VB6 developer, programming all kinds of applications. I also got involved in the development of a e-market in ASP.

When .Net was introduced, I had the privilege to start in a team of professionals and develop a framework. I learned a lot and started to work as a c#.Net consultant from then on.

After 2 years I also got the chance to work for several months with Sharepoint. Sharepoint kept chasing me all the years after and when the product got more and more mature I started to love it. Now Sharepoint is a somewhat daily occupation for me. I implemented a large quality document management system at PMRL being to only developper/technical analyst. Working close togehter with the QA department we succeeded in making and improving a very good Quality document management system. Its used up until this day.

I worked for 6 years as a .Net consultant for 2 of the largest consultancy companies in Belgium. Mainly I worked as a Sharepoint engineer, but also .Net expert and several ASP.Net and Windows applications. One of my last projects with the consultancy was creating a custom LIMS system. This was a succes and the customer (Phillip Morris) asked me to stay and help to expand and maintain this LIMS. I had great years in this company. There was a focus on innovation and a lot of opportunities for improving and developping new IS systems. Apart from several small projects I furter developped the custom LIMS system, created a SOP library based on Sharepoint and a web based Skill matrix system to handle the skills of the employees in the company.

Last I was involved in transferring the LIMS to a new lab in Singapore. This was done in a short time frame. The deployment was again a success and it passed the GLP inspection tests. I learned a lot from the Quality engineers about validation of computerized systems.

This year, new challenges are ahaed, I changed jobs. but still working with .Net and MS Sharepoint. The new company I'm working now is Galvapower. I find a great challenge and a good diversity in this company. I will be working mainly with .NET but also some VBA and ABAB will be on my dish.

My linkedIn!

 

 

Locations of visitors to this page