Solved

Multiple Threads Conflicting With Serialize/Deserialize

Posted on 2014-01-31
6
323 Views
Last Modified: 2014-02-01
I am using the example code below, for which an exception states "file is already in use."  Therefore, how can I prevent other threads from conflicting with a given serialize/deserialize?


<Serializable()>
Public Class serializedata
    Public Property arraya As Byte()
    Sub New(ByVal arraya() As Byte)
        Me.serarraya = arraya
        Dim stream As FileStream = File.Create(“c:\test.txt”)
        Dim formatter As New BinaryFormatter()
        formatter.Serialize(stream, Me)
        stream.Close()
    End Sub
End Class

Open in new window

0
Comment
Question by:lep1
  • 3
  • 3
6 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
Comment Utility
The role of a constructor is to build the object. As long as you are in the constructor, the object is not finished "constructing". So there are many operations that cannot be done in the constructor, and serializing it is one.

Why do you create a class only to serialize an array? Arrays are serializable, so you can simply serialize the array in the application where it is created.

		Dim arraya As Byte()
		Dim stream As IO.FileStream = IO.File.Create("c:\test.txt")
		Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
		formatter.Serialize(stream, arraya)
		stream.Close()

Open in new window

You do not need to create a class if you want to do that with multiple arrays. Simply create a method and call it with you array as a parameter, the same as you do with the constructor.
0
 

Author Comment

by:lep1
Comment Utility
Thanks, Ill move the serialization out of the constructor.  ( there are other types in the class which are not shown, and some are objects)
0
 

Author Comment

by:lep1
Comment Utility
What's the best way to return data from a deserialize class?  Should I create a method in the class and instantiate and then call the method from the code?  I read that constructors are not good at returning anything.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
It's not that constructors are not good at returning anything. It's that the instance is not finished building itself while in the constructor, so Me references something that is incomplete. You can use it to build the object, but very often cannot use any method in that object until the constructor has finished it's job.

Here is a constructor that I use when I want to build a collection (PhotoCollection) from a serialized file. I simply create another empty PhotoCollection inside of the constructor, deserialize into that object, and copy the data from the deserialized collection into the one that I am currently constructing.

<Serializable()> _
Partial Public Class PhotoCollection
	Inherits System.Collections.ObjectModel.Collection(Of Photo)

	Public Sub New(ByVal fileName As String)

		MyBase.New()

		If File.Exists(fileName) Then

			Dim binaryFile As FileStream
			Dim formatter As Formatters.Binary.BinaryFormatter
			Dim photos As PhotoCollection

			binaryFile = New FileStream(fileName, FileMode.Open)
			formatter = New Formatters.Binary.BinaryFormatter
			photos = DirectCast(formatter.Deserialize(binaryFile), PhotoCollection)

			For Each photo As Photo In photos
				Me.Add(photo)
			Next

		End If

	End Sub

End Class

Open in new window

0
 

Author Comment

by:lep1
Comment Utility
That's quite nice, but if I was accessing files inside the constructor and you mentioned not doing file access, aren't you accessing the disk as well?
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
I did not mention not doing file access. I said that you cannot serialize an object that is being constructed.

The things that you cannot do in a constructor are thing that have to do with the object being constructed. Everything else you can do.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now