C# ProgressBar with Deserialize()
Asked Answered
F

4

5

I have a few large object graphs which I have serialised, and some of them take a few moments to deserialise.

At this stage, I'm quite happy with my little "Please Wait..." box which appears and then disappears at completion, but I was just toying with the idea of having some sort of progress bar in the event that deserialisation starts taking longer.

It doesn't seem like there's any way to get the progress of the built-in Deserialize() method for the BinaryFormatter object. I suspect that had such a feature been included, there would be some sort of asynchronous callback which would poll whenever a block of bytes was read from the stream.

Have any of you seen an implementation of ProgressBar-like behaviour with object serialisation/deserialisation?

Fallon answered 20/3, 2011 at 23:14 Comment(1)
For anybody interested: I implemented this yesterday and am quite pleased with the result. The deserialisation happens in a separate thread, and I spool an additional progressbar thread which monitors the stream position/stream length and updates the progress bar. It works very well, and gives a fairly good approximation of deserialisation progress!Fallon
V
2

I've thought about this before- the only way that I can think of to even approximate it would be to wrap the Stream that is being deserialized and keep track of the position as the Formatter reads it. That assumes, however, that the Formatter reads linearly and continuously as it deserializes, and there is no guarantee that it does.

Valuation answered 20/3, 2011 at 23:18 Comment(1)
Yes, I thought of this also! :)Fallon
B
4

Stephen Toub discusses an approach to this problem in the .NET Matters column of the December 2006 MSDN Magazine.

He implements a wrapper around the stream which then allows intercepting of the Read method and raising a suitable event to signal progress.

Beatrix answered 27/8, 2012 at 5:42 Comment(1)
and now it's 2015, why is this not a standard part of .NET 4.5?Jestude
V
2

I've thought about this before- the only way that I can think of to even approximate it would be to wrap the Stream that is being deserialized and keep track of the position as the Formatter reads it. That assumes, however, that the Formatter reads linearly and continuously as it deserializes, and there is no guarantee that it does.

Valuation answered 20/3, 2011 at 23:18 Comment(1)
Yes, I thought of this also! :)Fallon
P
1

No I haven't. But you could make assumptions on how long the deserialization takes based on the size of the file you're deserializing and use that for your progress bar. This might give the user some indication on the time it will take, even though it's not accurate.

Phidippides answered 20/3, 2011 at 23:20 Comment(0)
M
0

Since I did some performance measurements on the deserializing of the large graph, I figured out that:

  • deriving from ISerializable and implementing GetObjectData() could be used as some kind of a 'lap counter', if you establish some estimate on how many times it would be called for each object type
  • implementing IDeserializationCallback.OnDeserialization() isn't usable at all because OnDeserialization is called right after EVERYTHING is loaded.

Well, since I also have large object graphs that load for several seconds, I'll try to implement some progress tracking using first method. Ping me if you want some info on how it goes.

As for the first suggestion, I wouldn't try to wrap it up into some descendand of Stream, I would rather use another thread and examine original source stream Length and Position and try to create some progress from that.

Morel answered 10/5, 2011 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.