> 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/