473,657 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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_Sen d), 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.Data Source = nothing, I get this
"Cross-thread operation not valid"-exception:

The problem is: I'm not using delegates, backgroundworke rs 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.Invalid OperationExcept ion' 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 5407
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.Invo ke 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**********@h otmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP03.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_Sen d), 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.Data Source = nothing, I get this
"Cross-thread operation not valid"-exception:

The problem is: I'm not using delegates, backgroundworke rs 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.Invalid OperationExcept ion'
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.CheckFo rIllegalCrossTh readCalls property as false. This is
however dangerous and can cause unspecified results. What you need to
do is to check for the Control.InvokeR equired 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.InvokeReq uired)
{
Invoke(new DoWorkHandler(t his.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.c om> wrote in message
news:uf******** ********@TK2MSF TNGP05.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
1470
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 it. Python : #!/usr/bin/python
0
983
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 threading no Events are received. Code:
6
345
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 following: I have: 1.A function that returns a Boolean 2.The Function calls different Sub Procedures on a variable numbered basis.
3
6198
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 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:
6
1092
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 other class to process data and populate the document with a report, then open the document. It all works great except the document opens and immediately closes. What I don't understand is this: If I Dim as New the document object in a...
7
2367
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 finished. I am trying to follow this example : http://www.codeproject.com/cs/miscctrl/progressdialog.asp But although the messages still get moved, the progress window never does anything. Here is my code in full, if anybody who knows...
2
1027
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 Responding" if the user clicks anywhere in the application. This is where my threading comes into place, replacing the troublesome "Not Responding" with a loading message in a form. However, the problem is that at program load/startup, my program...
0
1215
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 compiled python without threading support and am wondering what the repercussions will be (i don't want to enable threading for python space)
9
4107
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 in another class from the main class. So it couldn't access the Status Line or textbox. So what we did was set them up as properties: string IStatusDisplay.Status
0
8413
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.