473,385 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 5377
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Your Friend | last post by:
Hello All, I'm having issues capturing the output from a program while using threading. Program runs ok when I run without threading. Here's my Python code and the Java class that is called by...
0
by: BenLeino | last post by:
Hi out there, I have a little problem with threading an event receiving. I have a custom Class (DLL) that raises Events. When I run the Instance without threading it works fine. When I do...
6
by: John Rugo | last post by:
Hi All, I have the most basic understanding of using Threading. I have no problem creating a thread, addressing it to a Sub Procedure, and Starting the Thread. Works Great! My problem is the...
3
by: Pieter Coucke | last post by:
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...
6
by: tomb | last post by:
Hi all. I can't find anything to explain how to prevent losing my object reference. I have a function that instantiates a document object, passes it to another class, then uses a thread for the...
7
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
2
by: b0ssY | last post by:
Hi all, I have a problem with threading here. In my scenario, I need to do a very long operation after opening a file. Because of the long operation, it will cause the program to become "Not...
0
by: sndive | last post by:
I found nothing better to do than to start a new thread from c and run twisted event loop there. my question is: how can i safely call client.getPage() from another thread? could i? i have...
9
by: tshad | last post by:
I have a Windows App that is doing some work and then writing a "Now Processing..." line to the status line of the window as well as the Textbox on the form. But the problem is that the work is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.