471,852 Members | 1,054 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,852 software developers and data experts.

"Cross-thread operation not valid" without threading!!

Hi,

In my VB.NET 2005 application I'm generating and sending emails using the
outlook-object model (2003). When a mail is Send (MailObject_Send), I raise
an event in a global class, that is caught by all my forms that than refresh
the lists with emails.

But on the moment I do a MyDataGrid.DataSource = nothing, I get this
"Cross-thread operation not valid"-exception:

The problem is: I'm not using delegates, backgroundworkers or whatever! Why
does this happen? and what is the solution?

Thanks a lot in advance,

Pieter

The full exception:
A first chance exception of type 'System.InvalidOperationException' occurred
in System.Windows.Forms.dll

Additional information: Cross-thread operation not valid: Control ''
accessed from a thread other than the thread it was created on.
Apr 13 '06 #1
3 5249
Hi,

It can be that you receive the MailObject_Send event on a background thread.
If this is the case, check the InvokeRequired property on MyDataGrid and if
it's true, use MyDataGrid.Invoke to run the data source update code on the
UI thread.

BTW it would be better to use the SetDataBinding method to update the grid's
data source.

"Pieter Coucke" <pi**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi,

In my VB.NET 2005 application I'm generating and sending emails using the
outlook-object model (2003). When a mail is Send (MailObject_Send), I
raise an event in a global class, that is caught by all my forms that than
refresh the lists with emails.

But on the moment I do a MyDataGrid.DataSource = nothing, I get this
"Cross-thread operation not valid"-exception:

The problem is: I'm not using delegates, backgroundworkers or whatever!
Why does this happen? and what is the solution?

Thanks a lot in advance,

Pieter

The full exception:
A first chance exception of type 'System.InvalidOperationException'
occurred in System.Windows.Forms.dll

Additional information: Cross-thread operation not valid: Control ''
accessed from a thread other than the thread it was created on.


Apr 13 '06 #2
Hi

You cannot perform any action on Control based object from another
thread than the GUI thread unless you specify the
Control.CheckForIllegalCrossThreadCalls property as false. This is
however dangerous and can cause unspecified results. What you need to
do is to check for the Control.InvokeRequired property. If this
property is tru you'll need to call on Invoke for the same method.

Example:

private delegate void DoWorkHandler();

class MyForm : Form
{
// Method called from another thread than the GUI
DoWork()
{
if (this.InvokeRequired)
{
Invoke(new DoWorkHandler(this.DoWork), new object[] {}); //
here you put parameters needed for the DoWork method
}
else
{
// Perform gui thread actions
}
}

}
Thanks
Gumson

Apr 13 '06 #3
"Dmytro Lapshyn [MVP]" <x-****@no-spam-please.hotpop.com> wrote in message
news:uf****************@TK2MSFTNGP05.phx.gbl...
It can be that you receive the MailObject_Send event on a background
thread.


And why can this happen? I used a Delegate now, but I still don't get why I
had this thread-problem :-S
Apr 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Your Friend | last post: by
reply views Thread by BenLeino | last post: by
6 posts views Thread by John Rugo | last post: by
6 posts views Thread by tomb | last post: by
7 posts views Thread by Mike P | last post: by
reply views Thread by sndive | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
aboka
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.