Solved

Multiple Threads Conflicting With Serialize/Deserialize

Posted on 2014-01-31
6
330 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 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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

728 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