Solved

Multiple Threads Conflicting With Serialize/Deserialize

Posted on 2014-01-31
6
328 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net Configuration, settings file location? 8 42
Access Schema 6 45
Mobile apps - web/native or hybrid? 1 65
VB.Net  Exporting Excel Woorkbook to PDF 7 58
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 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