Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / June 2006

Tip: Looking for answers? Try searching our database.

how to resolve 'System.InvalidOperationException'

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mohan@tek.com - 24 May 2006 06:52 GMT
Hi all,

I have ported my VB.Net project from VS2003 to VS2005.

I am calling mainform instance (static variable) from other VB .net
modules
from the same projects.In VS2003 ,I did not face any issues. But in
VS2005 ,I
am getting the following exception.

"A first chance exception of type 'System.InvalidOperationException'
occurred in System.Windows.Forms.dll
Additional information: Cross-thread operation not valid: Control
'StressTest' accessed from a thread other than the thread it was
created on."

Thanks
Mohan
Barry Kelly - 24 May 2006 07:43 GMT
> I am calling mainform instance (static variable) from other VB .net
> modules
> from the same projects.In VS2003 ,I did not face any issues. But in
> VS2005 ,I
> am getting the following exception.

You got lucky. Your application has a bug.

> "A first chance exception of type 'System.InvalidOperationException'
> occurred in System.Windows.Forms.dll
> Additional information: Cross-thread operation not valid: Control
> 'StressTest' accessed from a thread other than the thread it was
> created on."

Like the error says, you can't access UI controls from threads that
didn't create them. Use Control.Invoke() to invoke a method on the UI
thread that owns the control, thereby marshalling the operation onto the
UI thread.

-- Barry

Signature

http://barrkel.blogspot.com/

mohan@tek.com - 15 Jun 2006 10:16 GMT
Thanks Barry. But I dont know how to access mainform instance and
functions using Control.invoke(). Also I do not know how to use
delegate for passing mainform's instance.  I have to call some methodes
written in main form.cs .

In 2003, I have declared a static variable for main form  and  using
that variable for calling static methods .

Can you please let me know some samples for the above one?

Thanks in advance
Mohan

> > I am calling mainform instance (static variable) from other VB .net
> > modules
[quoted text clipped - 16 lines]
>
> -- Barry
Barry Kelly - 15 Jun 2006 11:22 GMT
> Thanks Barry. But I dont know how to access mainform instance and
> functions using Control.invoke(). Also I do not know how to use
[quoted text clipped - 5 lines]
>
> Can you please let me know some samples for the above one?

Form descends from Control, so it has an Invoke() method too.

Here's an application which changes the color of the main form from a
background thread, written for .NET 1.1. The background thread is
created when the form becomes visible for the first time. It sleeps for
a few seconds, and then triggers the change.

The invocation of the method which calls methods on the UI (i.e. the App
instance) has to be marshaled across to the UI thread. This is done by
creating a delegate which refers to the method which will be accessing
the UI, and passing this delegate to Control.Invoke. App descends from
Form, which ultimately descends from Control, so it has an Invoke method
too.

---8<---
using System;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;

class App : Form
{
   static App _app;
   static Thread _backgroundThread;
   
   static void Main()
   {
       _app = new App();
       _app.VisibleChanged += new EventHandler(App_Shown);
       Application.Run(_app);
   }
   
   static void App_Shown(object sender, EventArgs e)
   {
       if (_backgroundThread == null && ((Form) sender).Visible)
       {
           _backgroundThread =
               new Thread(new ThreadStart(BackgroundThread));
           _backgroundThread.IsBackground = true;
           _backgroundThread.Start();
       }
   }
   
   static void BackgroundThread()
   {
       Thread.Sleep(TimeSpan.FromSeconds(3));
       _app.Invoke(new MethodInvoker(DoStuffOnUI));
   }
   
   static void DoStuffOnUI()
   {
       _app.BackColor = Color.Blue;
   }
}
--->8---

-- Barry

Signature

http://barrkel.blogspot.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.