?
Solved

Multiple Threads Conflicting With Serialize/Deserialize

Posted on 2014-01-31
6
Medium Priority
?
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

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

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

801 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