473,396 Members | 1,849 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,396 software developers and data experts.

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 3548
You should check if InvokeRequired is set on the ISynchronizeInvoke
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(ByVal sender As Object, ByVal args
As System.EventArgs)
If Not TestEventEvent Is Nothing Then
Dim singlecast As EventHandler

'Check each multi-cast delegate
For Each singlecast In TestEventEvent.GetInvocationList
Dim syncInvoke As System.ComponentModel.ISynchronizeInvoke
If TypeOf singlecast.Target Is
System.ComponentModel.ISynchronizeInvoke Then
syncInvoke = CType(singlecast.Target,
System.ComponentModel.ISynchronizeInvoke)
End If
If Not syncInvoke Is Nothing Then
If syncInvoke.InvokeRequired Then
syncInvoke.Invoke(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******@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.com...
You should check if InvokeRequired is set on the ISynchronizeInvoke
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(ByVal sender As Object, ByVal args As System.EventArgs)
If Not TestEventEvent Is Nothing Then
Dim singlecast As EventHandler

'Check each multi-cast delegate
For Each singlecast In TestEventEvent.GetInvocationList
Dim syncInvoke As System.ComponentModel.ISynchronizeInvoke
If TypeOf singlecast.Target Is
System.ComponentModel.ISynchronizeInvoke Then
syncInvoke = CType(singlecast.Target,
System.ComponentModel.ISynchronizeInvoke)
End If
If Not syncInvoke Is Nothing Then
If syncInvoke.InvokeRequired Then
syncInvoke.Invoke(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.com> wrote in message
news:O4*************@TK2MSFTNGP15.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.com> wrote in message
news:ee**************@tk2msftngp13.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.com> wrote in message
news:O4*************@TK2MSFTNGP15.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
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...
1
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...
8
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...
11
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#...
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...
3
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....
5
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...
1
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...
11
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...
2
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." ...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.