473,799 Members | 3,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cross Thread Operation not valid.

Bob
Hi,
Moving a project from .net 2003 -> 2005 Beta 2
Windows App. Main Window is start object.
Main window spawns a thread.
After doing some work this thread raises an interrupt.
The event carries a reference to the class that raised it plus an
instantiated, empty eventarg.
RaiseEvent LineStateChange (Me, e)

The 'RaiseEvent' errors in 2005 with Cross thread...

Any ideas on what is going on here?
Thanks
Bob
Nov 21 '05 #1
4 3579
You should check if InvokeRequired is set on the ISynchronizeInv oke
interfaced of your event sink before raising the event from a separate
thread. If this event sink requires synchronization , you'll need to call
Invoke instead. Here is some sample code of a OnTestEvent that implements
this.

Public Event TestEvent as EventHandler

Protected Overridable Sub OnTestEvent(ByV al sender As Object, ByVal args
As System.EventArg s)
If Not TestEventEvent Is Nothing Then
Dim singlecast As EventHandler

'Check each multi-cast delegate
For Each singlecast In TestEventEvent. GetInvocationLi st
Dim syncInvoke As System.Componen tModel.ISynchro nizeInvoke
If TypeOf singlecast.Targ et Is
System.Componen tModel.ISynchro nizeInvoke Then
syncInvoke = CType(singlecas t.Target,
System.Componen tModel.ISynchro nizeInvoke)
End If
If Not syncInvoke Is Nothing Then
If syncInvoke.Invo keRequired Then
syncInvoke.Invo ke(singlecast, New Object() {Me, New
EventArgs})
Else
singlecast(Me, Nothing)
End If
End If
Next
End If
End Sub

"Bob" wrote:
Hi,
Moving a project from .net 2003 -> 2005 Beta 2
Windows App. Main Window is start object.
Main window spawns a thread.
After doing some work this thread raises an interrupt.
The event carries a reference to the class that raised it plus an
instantiated, empty eventarg.
RaiseEvent LineStateChange (Me, e)

The 'RaiseEvent' errors in 2005 with Cross thread...

Any ideas on what is going on here?
Thanks
Bob

Nov 21 '05 #2
Bob
Hi,
Thanks for the snippet.
Based on this and some other sample code I ended up hatching a delegate in
the main window and invoking the delegate in the worker thread.
regards
Bob
"TrtnJohn" <Tr******@discu ssions.microsof t.com> wrote in message
news:36******** *************** ***********@mic rosoft.com...
You should check if InvokeRequired is set on the ISynchronizeInv oke
interfaced of your event sink before raising the event from a separate
thread. If this event sink requires synchronization , you'll need to call
Invoke instead. Here is some sample code of a OnTestEvent that implements
this.

Public Event TestEvent as EventHandler

Protected Overridable Sub OnTestEvent(ByV al sender As Object, ByVal args As System.EventArg s)
If Not TestEventEvent Is Nothing Then
Dim singlecast As EventHandler

'Check each multi-cast delegate
For Each singlecast In TestEventEvent. GetInvocationLi st
Dim syncInvoke As System.Componen tModel.ISynchro nizeInvoke
If TypeOf singlecast.Targ et Is
System.Componen tModel.ISynchro nizeInvoke Then
syncInvoke = CType(singlecas t.Target,
System.Componen tModel.ISynchro nizeInvoke)
End If
If Not syncInvoke Is Nothing Then
If syncInvoke.Invo keRequired Then
syncInvoke.Invo ke(singlecast, New Object() {Me, New EventArgs})
Else
singlecast(Me, Nothing)
End If
End If
Next
End If
End Sub

"Bob" wrote:
Hi,
Moving a project from .net 2003 -> 2005 Beta 2
Windows App. Main Window is start object.
Main window spawns a thread.
After doing some work this thread raises an interrupt.
The event carries a reference to the class that raised it plus an
instantiated, empty eventarg.
RaiseEvent LineStateChange (Me, e)

The 'RaiseEvent' errors in 2005 with Cross thread...

Any ideas on what is going on here?
Thanks
Bob

Nov 21 '05 #3
Bob
Hi,
As stated further down in this thread. (Oh No , that word again.) I have a
solution that involves the working thread invoking a delegate in the calling
thread (main Window.)
However this troubles me.
There seems to be too much coupling IMHO.
In .net 2003, rightly or wrongly, I could design my worker class without
prior knowledge of the calling class. It could be designed to do its work
then raise its Publicly declared event with its payload.
The event handler on the main window could be designed later.
Now it seems I have to know about the main form's delegate before I can
write the beginInvoke instruction in the worker class.
I hope I have got it wrong and someone can enlighten me. Otherwise this
seems a large leap backwards.
regards
Bob
"Bob" <bo*@nowhere.co m> wrote in message
news:O4******** *****@TK2MSFTNG P15.phx.gbl...
Hi,
Moving a project from .net 2003 -> 2005 Beta 2
Windows App. Main Window is start object.
Main window spawns a thread.
After doing some work this thread raises an interrupt.
The event carries a reference to the class that raised it plus an
instantiated, empty eventarg.
RaiseEvent LineStateChange (Me, e)

The 'RaiseEvent' errors in 2005 with Cross thread...

Any ideas on what is going on here?
Thanks
Bob

Nov 21 '05 #4
Bob
Hi,
Please ignore this bleat.
I have actually read the documenation and found the correct 'uncoupled' way
of doing events.
Thanks
Bob
"Bob" <bo*@nowhere.co m> wrote in message
news:ee******** ******@tk2msftn gp13.phx.gbl...
Hi,
As stated further down in this thread. (Oh No , that word again.) I have a
solution that involves the working thread invoking a delegate in the calling thread (main Window.)
However this troubles me.
There seems to be too much coupling IMHO.
In .net 2003, rightly or wrongly, I could design my worker class without
prior knowledge of the calling class. It could be designed to do its work
then raise its Publicly declared event with its payload.
The event handler on the main window could be designed later.
Now it seems I have to know about the main form's delegate before I can
write the beginInvoke instruction in the worker class.
I hope I have got it wrong and someone can enlighten me. Otherwise this
seems a large leap backwards.
regards
Bob
"Bob" <bo*@nowhere.co m> wrote in message
news:O4******** *****@TK2MSFTNG P15.phx.gbl...
Hi,
Moving a project from .net 2003 -> 2005 Beta 2
Windows App. Main Window is start object.
Main window spawns a thread.
After doing some work this thread raises an interrupt.
The event carries a reference to the class that raised it plus an
instantiated, empty eventarg.
RaiseEvent LineStateChange (Me, e)

The 'RaiseEvent' errors in 2005 with Cross thread...

Any ideas on what is going on here?
Thanks
Bob


Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2306
by: Mikael Syska | last post by:
Hey, Cross-thread operation not valid: Control 'ThreadStartedAt' accessed from a thread other than the thread it was created on I get this Exception when I try to update some labels/textbox's from a Thread... I have read something about that I can use Delegates/Invoke and stuff in that direction, but I'm not that far in my book now that I understand how all that works....
1
9499
by: Mesan | last post by:
I'm getting a "Cross-thread operation not valid" Exception and I can't figure out why. I've got a BackgroundWorker that creates a UserControl with a whole lot of other user controls inside of it, full of data I'm getting from the database. When my background worker completes I take e.Result and cast it back into a UserControl. I create a TabPage, add the returned UserControl to the Controls of the new TabPage and *BAM* there's my...
8
4856
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item I want to raise the ProgressChanged-event to update the DataGridView. It works fine when only one Progresschanged is fired, but at the second, third, fopurth etc it raises everytile a 'Cross-thread operation not valid"-exception on lmy...
11
8690
by: HairlipDog58 | last post by:
Hello, There are several 'cross-thread operation not valid' exception postings in the MSDN discussion groups, but none address my exact situation. I have authored a .NET component in Visual C# .NET 2003 that is capable of firing several types of events to notify user of errors, data changes, etc. The component uses a thread to perform background communications and if there is an error or data-change fires an event to notify the user....
3
5423
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:
3
30556
by: Le Minh | last post by:
I receive this message: "System.InvalidOperationException: Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on" .... this is a exception. Can fix it?
5
3606
by: nospam | last post by:
Hi all. I have encountered a "Cross-thread operation not valid" error in a test program I created and although I have came up with a solution I don't like the performance of the program. I hope perhaps some experts here can help me out. Here is what my program consists of: 1) A form containng several tabs, one of which contains a ListView
1
1909
Raventara
by: Raventara | last post by:
Hi peoples.... This is a pain in the ass: Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on. I am trying to update the text in the textbox when data is recieved on the new SerialPort control. So I added the following: Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
11
6902
by: Ahmedhussain | last post by:
hey everyone... I have a code which gives me this error. Error : Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on. I am actually populating a user control dynamically. And the problem arises when i try to populate it with values. and the code runs again after 30 or more seconds to get updated values and so on.. The code is given below This is actually the user control...
2
5962
by: bhupendrakumar | last post by:
error message System.InvalidOperationException was unhandled Message="Cross-thread operation not valid: Control 'listView2' accessed from a thread other than the thread it was created on." Source="System.Windows.Forms" StackTrace: at System.Windows.Forms.Control.get_Handle() at System.Windows.Forms.Control.SendMessage(Int32 msg, Int32 wparam, Int32 lparam) at...
0
9687
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
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10228
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,...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5463
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
4141
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
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
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.