KEMBAR78
Complete-Reference-Vb Net 76 | PDF | Computer File | Information Technology Management
0% found this document useful (0 votes)
23 views1 page

Complete-Reference-Vb Net 76

Uploaded by

khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views1 page

Complete-Reference-Vb Net 76

Uploaded by

khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Activating Serialization at Run Time

That's all there is to serializing object data out to some persistent storage location. Instantiating the class
automatically provides the serialization support.

Activating Serialization at Run Time

There is no code in the BaseNodeContainer class that fires serialization events (that automatically calls the
SerializeOut method), so at first we will have to do it manually from the application user interface, which we
will create with many of the resources we will learn about in the next chapter. And, we could easily add a
timed interval event that calls the PlayOutList method every few seconds or minutes.

In the meantime, the following line of code triggers the serialization manually:

List.PlayOutList("c:\indexworks\workfile.txt")

We simply call the method and pass it the target file information. This method then invokes the serialization
process in the class, and that's all there is to it.

Now what happens if the application is shut down (or the machine shuts down abnormally while we are in the
middle of a read (a power failure or something))? How do we get back the data we had when we first started
up or last wrote to the List object? Reconstituting the object is as easy as spewing out to the file. We need to
create a method to implements the deserialization. Follow these steps to provide this:

1. Check if an object was serialized to disk. This could be as simple as checking for the target file.
2. Create a holding object in the application of the type to receive the data from the file.
3. Create a SOAP formatter to recover the data from the file.
4. Open the file.
5. Deserialize into the holding object.

This is demonstrated in the followed deserializer method, which can be provided by an external facility, like
the application's startup object or the user interface:

Public Sub PlayInList(ByVal source As String)


Try
If File.Exists(source) Then
Dim fileForRecover As Stream = File.OpenRead(source)
Dim aClone As New BaseNodeCollection()
Dim meData As New SoapFormatter()
aClone = CType(meData.Deserialize(fileForRecover), _
BaseNodeCollection)
fileForRecover.Close()
List = aClone
End If
Catch E As Exception
Me.exceptinfo = E.Message
End Try
End Sub

If all works well the linked list gets sucked off the hard disk and is streamed into the application, just in time
to service the first Web surfer's hit on the search page. What's cooking in the PlayInList method? First, we
needed to create a new Stream to open the file in the source folder. The code that achieves this can be written
as follows:

Dim fileForRecover As Stream = File.OpenRead(source)

560

You might also like