Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

VS 2010 Asynchronoous Serialization

$
0
0
I would like to implement an automatic backup function in my software where the main object gets serialized and stored asynchronously on a timed schedule, but do it asynchronously so as to not slow down the main program.

Currently, my serialization/saving function looks like this:
.net Code:
  1. Public Function Serialize(FileName As String, Obj As Object, XML As Boolean) As Boolean
  2.         ' Serializes an object. XML = True if XML serializing, otherwise Binary. Returns True if no errors.
  3.         Dim S As FileStream = Nothing
  4.         Dim F As Object
  5.         Serialize = True
  6.  
  7.         Try
  8.             If XML Then
  9.                 F = New Xml.Serialization.XmlSerializer(Obj.GetType)
  10.             Else
  11.                 F = New BinaryFormatter
  12.             End If
  13.             S = New FileStream(FileName, FileMode.Create)
  14.             F.Serialize(S, Obj)
  15.         Catch ex As Exception
  16.             MessageBox.Show("Error Saving '" & ShortenPath(FileName) & "'" & vbNewLine & vbNewLine & ex.Message, "File Save Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
  17.             Serialize = False
  18.         Finally
  19.             If S IsNot Nothing Then S.Close()
  20.         End Try
  21. End Function

To save any class as XML, I just call from within the class:
.net Code:
  1. Success = Serialize(FileName, Me, True)

So what is the best way to make the Serialize function work the same as it does, but asynchronously?

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>