相同的代码:一旦使用InvalidOperationEcxeption,一次没有

时间:2017-08-30 08:06:11

标签: .net vb.net visual-studio-2012

我有两个项目。两者都有完全相同的代码。通常,它应该加载文件,对文本进行一些更改并将其保存到新文件中。这应该是异步的,以便在工作时更改光标并禁用按钮。 完成后,表单应该由于回调函数

而将这些内容设置回来

Class Form1

Imports System.Runtime.Remoting.Messaging
Imports System.Threading
Imports System

Public Class Form1
Public oldName As String
Public oldFile As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim ad As New AsyncSerialize()
    Dim caller As New asyncMethodCaller(AddressOf ad.doTasks)
    Dim dummy As Integer = 0
    Dim Dialog As New OpenFileDialog
    If Dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim CloseDialog As New SaveFileDialog
        CloseDialog.Filter = "ssd|*.ssd|scd|*.scd|icd|*.icd|iid|*iid"
        If CloseDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            If System.IO.File.Exists(CloseDialog.FileName) Then
                oldName = CloseDialog.FileName
                oldFile = System.IO.File.ReadAllText(CloseDialog.FileName)
            End If
            Button1.Enabled = False
            Me.Cursor = Cursors.WaitCursor
            caller.BeginInvoke(Dialog.FileName, CloseDialog.FileName, AddressOf stopCursor, Nothing)
        Else
            System.Windows.Forms.MessageBox.Show("Datei wurde NICHT erfolgreich gespeichert", "OK", MessageBoxButtons.OK)
        End If
    End If
End Sub


Private Sub stopCursor(ByVal ar As IAsyncResult)
    Dim result As AsyncResult = CType(ar, AsyncResult)
    Dim caller As asyncMethodCaller = CType(result.AsyncDelegate, asyncMethodCaller)
    If caller.EndInvoke(ar) Then
        System.Windows.Forms.MessageBox.Show("Datei wurde erfolgreich gespeichert", "OK", MessageBoxButtons.OK)
    Else
        If oldFile IsNot Nothing Then
            System.IO.File.WriteAllText(oldName, oldFile)
        End If
        System.Windows.Forms.MessageBox.Show("Datei wurde NICHT erfolgreich gespeichert", "OK", MessageBoxButtons.OK)
    End If
    Button1.Enabled = True
    Me.Cursor = Cursors.Default
End Sub

End Class

Class AsyncSerialize

Imports System.Runtime.InteropServices
Imports System.Threading

Public Class AsyncSerialize

Public Function doTasks(filename As String, savename As String) As Boolean
    Dim fs As System.IO.FileStream
    Try
        fs = System.IO.File.Open(filename, IO.FileMode.Open)
        Dim serializer As New System.Xml.Serialization.XmlSerializer(GetType(ESL.SCL.SCL))
        Dim SCL As ESL.SCL.SCL
        SCL = serializer.Deserialize(fs)
        fs.Close()
        Dim Exporter As WMSExporter = New WMSExporter
        Dim SCLWMS As ESL.SCL.SCL = Exporter.Export(SCL)

        fs = System.IO.File.Create(savename)
        serializer.Serialize(fs, SCLWMS)

        fs.Close()
        Return True
    Catch ex As Exception
        System.IO.File.Delete(savename)
        Return False
    Finally
        If fs IsNot Nothing Then
            fs.Close()
        End If
    End Try
End Function
End Class

Public Delegate Function asyncMethodCaller(ByVal filename As String, savename As String) As Boolean

当我运行这些项目时,一个项目运行没有任何问题,并做它应该做的事情。当我尝试设置Button1.Enabled = True时,另一个项目抛出InvalidOperationException。

任何想法为什么相同的代码一次又一次产生错误? 我怀疑配置文件中必定存在差异。这可能是问题吗?

0 个答案:

没有答案