Serialization and Events

H

herbert

I have a console project that manages an object tree:
dim withevents myObj as myClass

1) Using the binary serializer I can serialize and deserialize the object
tree without any error message - I was expecting .NET 3.x to give at least a
warning about event consumers.
I have to use binary serializer because of circular references inside the
object hierarchy.

2) after deserialization, inside the object trees all events are correctly
propagated up to the highest object, yet not to the event handler in the
console app. Which is correct.

3) after deserialization I use AddHandler myObj ... which does not result in
any event delivered.

questions:
Q1: What is missing to make AddHandler work?
Q2: Is the recipe by Mr. Rocky Lhotka about using custom events from 2004
http://www.lhotka.net/WeBlog/CommentView.aspx?guid=776f44e8-aaec-4845-b649-e0d840e6de2c
still best practice?


thank you very much. herbert
 
H

herbert

another twist to that:

in the following code, where simple objects are serialized (no object
hierarchy, no circular references),
1) the deserialization works as expected, ie. the events work afterwards
without adding event handlers.

2) what's more: setting
mObj1 = nothing
does not eliminate the event delegate as I expected. So when raising
mObj.E01 the console app receives the event twice.

Back to the original question:
What is the best way to serialized an object hierarchy and obtain its events
after deserialization in a Windows Service or Console App (no WinForm app)?

thank you very much. herbert

Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO

Module Module2
Const SERIALIZEDFILE1 As String = "d:\temp\Serialized1.bin"
Const SERIALIZEDFILE2 As String = "d:\temp\Serialized2.bin"

Dim WithEvents mObj1 As New Class1("Event from obj1")
Dim mObj2 As New Class1("Event from obj2")

Sub Main()
AddHandler mObj2.E01, AddressOf Event_Handler

'this delivers:
' Event happened: Event from obj1
' Event happened: Event from obj2
mObj1.OnEvent()
mObj2.OnEvent()

Serialize(mObj1, SERIALIZEDFILE1)
Serialize(mObj2, SERIALIZEDFILE2)

RemoveHandler mObj1.E01, AddressOf Event_Handler
RemoveHandler mObj2.E01, AddressOf Event_Handler

mObj1 = Nothing
mObj2 = Nothing

mObj1 = Deserialize(SERIALIZEDFILE1)
mObj2 = Deserialize(SERIALIZEDFILE2)

'this delivers:
' Event happened: Event from obj1
' Event happened: Event from obj1
' Event happened: Event from obj2
mObj1.OnEvent()
mObj2.OnEvent()

Console.ReadLine()
End Sub

Sub Serialize(ByVal o As Object, ByVal filename As String)
'serialisieren in binärdatei
Dim form As New BinaryFormatter()
Dim stream As FileStream
stream = New FileStream(filename, FileMode.Create, FileAccess.Write,
FileShare.None)
form.Serialize(stream, o)
stream.Close()
Console.WriteLine("Saved to " & filename)
End Sub

Function Deserialize(ByVal filename As String) As Object
Dim o As Object
Dim form As New BinaryFormatter()
Dim stream As FileStream
stream = New FileStream(filename, FileMode.Open, FileAccess.Read,
FileShare.Read)
o = form.Deserialize(stream)
stream.Close()
Console.WriteLine("Loaded from " & filename)
Return o
End Function

Public Sub Event_Handler(ByVal name As String) Handles mObj1.E01
Console.WriteLine("Event happened: " & name)
End Sub

<Serializable()> _
Public Class Class1
Private mName As String
Public Event E01(ByVal name As String)

Public Sub New(ByVal name As String)
mName = name
End Sub

Public Sub OnEvent()
RaiseEvent E01(mName)
End Sub

End Class
End Module
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top