二进制格式化程序返回调用者并取消form.close

时间:2014-08-26 07:32:01

标签: c# .net debugging serialization binaryformatter

我想用二进制格式化程序序列化一个结构数组,以便通过网络发送它或在这种情况下将其保存到文件中。
它订阅了我的表单的 Form.Closing 事件

void writeHistoryToFile(object sender, CancelEventArgs e)
{
    ListView.ListViewItemCollection coll = historyListView.Items;
    int count = coll.Count;
    if(count == 0)
        return;
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream memStr = new MemoryStream();
    searchResult[] container = new MainForm.searchResult[count];
    for(int i = 0; i < count; i++)
    {
        searchResult tagged = (searchResult)coll[i].Tag;
        container[i] = tagged;
    }
    byte[] bytesToWrite;
    bf.Serialize(memStr, container);   //HERE
    bytesToWrite = memStr.ToArray();   //BREAKPOINT

    List<FileInfo> hisFls = historyFiles;
    if(hisFls.Count != 0)
    {
        foreach(FileInfo element in hisFls)
        {
            element.Delete();
        }
    }

    FileInfo serFile = getTempFile(".avsh");
    using(FileStream writeStream = serFile.OpenWrite())
    {
        writeStream.Write(bytesToWrite, 0, bytesToWrite.Length);
        writeStream.Flush();
    }
}

它的作用:
我关闭表单并调用函数(触发设置在HERE上方的断点) 当松动时,表格不会关闭并再次响应 单步执行代码时,调试器很快挂起,然后代码再次连续运行 从未达到HERE背后设置的断点。

所以似乎二进制格式化程序返回函数或其他东西,但没有任何Exception等。

UPATE
看起来我的结构不可序列化,呃 添加了[Serializable],现在可以使用了。请参阅有关为何没有例外等的评论。-_- Errormessage和stacktrace

The Type "ExportGrepper.MainForm+searchResult" in Assembly Arbeitsvorräte duchsuchen, Version=1.0.5351.15603, Culture=neutral, PublicKeyToken=null" is not marked as serializable.

   at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)

   at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)

   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()

   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Type objectType, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter)

   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Type objectType, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter)

   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray(WriteObjectInfo objectInfo, NameInfo memberNameInfo, WriteObjectInfo memberObjectInfo)

   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)

   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)

   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)

   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)

   at ExportGrepper.MainForm.writeHistoryToFile(Object sender, CancelEventArgs e)

1 个答案:

答案 0 :(得分:1)

“答案”能够关闭此问题: 尝试在代码中添加Try..Catch并检查发生了哪个错误。 然后使您的结构可序列化。 ;)