C # ProgressBar with deserialization ()

I have several large object graphs that I serialize, and some of them take a few seconds to deserialize.

At this point, I am quite happy with my little “Please wait ...” field, which appears and then disappears at the end, but I just played with the idea of ​​having some kind of progress indicator in case deserialization starts longer.

There seems to be no way to get the progress of the built-in Deserialize() method Deserialize() BinaryFormatter object. I suspect that if such a function were enabled, there would be some kind of asynchronous callback that will interrogate whenever a block of bytes is read from the stream.

Have any of you seen implementing a behavior like ProgressBar with object serialization / deserialization?

+4
source share
4 answers

I thought about this before - the only way I can think of to even get close to it would be to crop the Stream that is being deserialized and track the position when Formatter reads it. This suggests, however, that Formatter reads linearly and continuously as it is deserialized, and there is no guarantee that it does.

+1
source

Stephen Tuub discusses an approach to this issue in the .NET Matters column in the December issue of MSDN .

It implements a wrapper around the stream, which then allows you to intercept the Read method and raise a suitable event to make progress.

+2
source

No no. But you can make assumptions about how long deserialization is performed based on the size of the file you are deserializing, and use it for your progress bar. This may give the user some indication of the time it will take, even if it is inaccurate.

+1
source

Since I took some performance measurements when deserializing a large graph, I realized that:

  • arising from ISerializable , and the implementation of GetObjectData() can be used as some GetObjectData() “cap counter” if you set some estimate of how many times it will be called for each type of object
  • the implementation of IDeserializationCallback.OnDeserialization() not applicable at all, because OnDeserialization is called immediately after loading TOTAL.

Well, since I also have large graphical objects that load in a few seconds, I will try to implement progress tracking using the first method. Ping me if you want to know how this happens.

As for the first sentence, I would not wrap it in some descendand Stream , I would prefer to use a different stream and study the source stream of the Length and Position source and try to create some progress from this.

0
source

Source: https://habr.com/ru/post/1344516/


All Articles