Stream.WriteTo 锁定线程

发布于 2024-12-02 10:04:43 字数 777 浏览 0 评论 0原文

我有这样的代码:

public static void SerializeRO(Stream stream, ReplicableObject ro) {
    MemoryStream serializedObjectStream = new MemoryStream();
    Formatter.Serialize(serializedObjectStream, ro);

    MemoryStream writeStream = new MemoryStream();
    BinaryWriter bw = new BinaryWriter(writeStream);

    bw.Write(serializedObjectStream.Length);
    serializedObjectStream.Seek(0, SeekOrigin.Begin);
    serializedObjectStream.WriteTo(writeStream);
    serializedObjectStream.Close();

    writeStream.WriteTo(stream);

    bw.Close();
}

writeStream.WriteTo(stream); 行永远不会完成。程序到达那条线并且不会继续进行。

stream 始终是 NetworkStream。我已经检查过,我认为它是一个有效的对象(至少它不是 null 也不是已处置)。

那么到底是怎么回事呢?

I have this code:

public static void SerializeRO(Stream stream, ReplicableObject ro) {
    MemoryStream serializedObjectStream = new MemoryStream();
    Formatter.Serialize(serializedObjectStream, ro);

    MemoryStream writeStream = new MemoryStream();
    BinaryWriter bw = new BinaryWriter(writeStream);

    bw.Write(serializedObjectStream.Length);
    serializedObjectStream.Seek(0, SeekOrigin.Begin);
    serializedObjectStream.WriteTo(writeStream);
    serializedObjectStream.Close();

    writeStream.WriteTo(stream);

    bw.Close();
}

The line writeStream.WriteTo(stream); never finishes. The program gets to that line and won't progress.

stream is always a NetworkStream. I've checked and I think it's a valid object (at least it's not null nor disposed).

So what's going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坠似风落 2024-12-09 10:04:43

我尝试了你的代码 - 写入 FileStream - 并且我总是将零字节写入流。我不知道为什么当没有什么可写时 WriteTo 会在 NextWorkStream 上阻塞,但这可能是一个问题

当我从 MemoryStream< 中提取字节时/code> 并直接编写它们以流式传输所有内容,例如(我正在例程中创建一个二进制格式化程序,看来您已经有了一个格式化程序)。

public static void SerializeRO(Stream stream, object ro)
{
    MemoryStream serializedObjectStream = new MemoryStream();
    var f = new BinaryFormatter();
    f.Serialize(serializedObjectStream, ro);

    MemoryStream writeStream = new MemoryStream();
    BinaryWriter bw = new BinaryWriter(writeStream);

    var bytes = serializedObjectStream.ToArray();
    bw.Write(bytes.Length);
    bw.Write(bytes);

    var bwBytes = writeStream.ToArray();

    stream.Write(bwBytes, 0, bwBytes.Length);

    bw.Close();
}

不过,我会用一个 MemoryStream 这样做,并直接写入 stream,除非有一些我不知道的 NetworkStream (这当然会关闭 stream 这可能不是您想要的)

public static void SerializeRO(Stream stream, object ro)
{
    byte[] allBytes;
    using (var serializedObjectStream = new MemoryStream())
    {
        var f = new BinaryFormatter();
        f.Serialize(serializedObjectStream, ro);
        allBytes = serializedObjectStream.ToArray();
    }
    using (var bw = new BinaryWriter(stream))
    {
        bw.Write(allBytes.Length);
        bw.Write(allBytes);
    }
}

不会关闭 NetworkStream 的版本(只是不要将二进制编写器放在 using 语句或 Close()它)

public static void SerializeRO(Stream stream, object ro)
{
    byte[] allBytes;
    using (var serializedObjectStream = new MemoryStream())
    {
        var f = new BinaryFormatter();
        f.Serialize(serializedObjectStream, ro);
        allBytes = serializedObjectStream.ToArray();
    }
    var bw = new BinaryWriter(stream)
    bw.Write(allBytes.Length);
    bw.Write(allBytes);
}

I tried your code - writing to a FileStream - and I always get zero bytes written to the stream. I don't know why WriteTo would block on a NextWorkStream when there's nothing to write, but that could be a problem

When I extract the bytes from the MemoryStream and write them directly to stream everything works e.g. (I'm creating a binary formatter in the routine, it seems you already have a formatter).

public static void SerializeRO(Stream stream, object ro)
{
    MemoryStream serializedObjectStream = new MemoryStream();
    var f = new BinaryFormatter();
    f.Serialize(serializedObjectStream, ro);

    MemoryStream writeStream = new MemoryStream();
    BinaryWriter bw = new BinaryWriter(writeStream);

    var bytes = serializedObjectStream.ToArray();
    bw.Write(bytes.Length);
    bw.Write(bytes);

    var bwBytes = writeStream.ToArray();

    stream.Write(bwBytes, 0, bwBytes.Length);

    bw.Close();
}

However I'd do it like this with one MemoryStream and writing directly to stream, unless there's something I don't know about NetworkStream (this will of course close stream which may not be what you want)

public static void SerializeRO(Stream stream, object ro)
{
    byte[] allBytes;
    using (var serializedObjectStream = new MemoryStream())
    {
        var f = new BinaryFormatter();
        f.Serialize(serializedObjectStream, ro);
        allBytes = serializedObjectStream.ToArray();
    }
    using (var bw = new BinaryWriter(stream))
    {
        bw.Write(allBytes.Length);
        bw.Write(allBytes);
    }
}

Version that won't close your NetworkStream (just don't put the binary writer in a using statement or Close() it)

public static void SerializeRO(Stream stream, object ro)
{
    byte[] allBytes;
    using (var serializedObjectStream = new MemoryStream())
    {
        var f = new BinaryFormatter();
        f.Serialize(serializedObjectStream, ro);
        allBytes = serializedObjectStream.ToArray();
    }
    var bw = new BinaryWriter(stream)
    bw.Write(allBytes.Length);
    bw.Write(allBytes);
}
变身佩奇 2024-12-09 10:04:43

只是我的想法:在将流写入另一个流之前,尝试关闭您的 BinaryWriter 。当您的写入器打开时,流尚未完成,因此 WriteTo newer 将找到流的末尾。

Just my thoughts: try to close your BinaryWriter before writing stream to another. While your writer is opened stream is not finished and as the result WriteTo newer will find the end of the stream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文