Solved

Multiple Threads Conflicting With Serialize/Deserialize

Posted on 2014-01-31
6
324 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
ID: 39825811
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
ID: 39826243
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
ID: 39826564
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40
ID: 39826654
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
ID: 39826883
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
ID: 39826996
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

948 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

18 Experts available now in Live!

Get 1:1 Help Now