WaitCursor在运行时

时间:2017-06-26 08:32:31

标签: vb.net

我有以下问题:

序列化需要一些时间,我想在那段时间将Cursor更改为WaitCursor,因此用户知道程序仍在工作而不会崩溃。 但是在序列化时,程序似乎会自动将Cursor更改为Default。所以在这种情况下我根本不会看到WaitCursor。如果我删除Me.Cursor = Cursors.Default,则在保存新文件后仅显示WaitCursor。

有没有办法解决这个问题并在程序运行时显示WaitCursor?

If Dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.Cursor = Cursors.WaitCursor
            Dim fs As System.IO.FileStream = System.IO.File.Open(Dialog.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)
            Me.Cursor = Cursors.Default

            Dim CloseDialog As New SaveFileDialog
            CloseDialog.Filter = "ssd|*.ssd|scd|*.scd|icd|*.icd|iid|*iid"
            If CloseDialog.ShowDialog = Windows.Forms.DialogResult.OK Then

                fs = System.IO.File.Create(CloseDialog.FileName)
                SCLWMS.revision = "A"
                serializer.Serialize(fs, SCLWMS)

                fs.Close()
                System.Windows.Forms.MessageBox.Show("Datei wurde erfolgreich gespeichert", "OK", MessageBoxButtons.OK)
            Else
                System.Windows.Forms.MessageBox.Show("Datei wurde NICHT erfolgreich gespeichert", "OK", MessageBoxButtons.OK)
            End If
    End If

我也尝试过异步工作:

      If Dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
        Me.Cursor = Cursors.WaitCursor
        thread = New System.Threading.Thread(AddressOf load_scl)
        thread.Start(Dialog.FileName)


        thread.Join()
        Me.Cursor = Cursors.Default
      End if

Private Sub load_scl(filename As String)
    Dim fs As System.IO.FileStream = 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)
    'Me.UseWaitCursor = False
    Dim CloseDialog As New SaveFileDialog
    CloseDialog.Filter = "ssd|*.ssd|scd|*.scd|icd|*.icd|iid|*iid"
    If CloseDialog.ShowDialog = Windows.Forms.DialogResult.OK Then

        fs = System.IO.File.Create(CloseDialog.FileName)
        SCLWMS.revision = "A"
        serializer.Serialize(fs, SCLWMS)

        fs.Close()
        System.Windows.Forms.MessageBox.Show("Datei wurde erfolgreich gespeichert", "OK", MessageBoxButtons.OK)
    Else
        System.Windows.Forms.MessageBox.Show("Datei wurde NICHT erfolgreich gespeichert", "OK", MessageBoxButtons.OK)
    End If
End Sub

这里的问题是,我得到一个ThreadStateException。 所以我认为可能有一个更容易的解决方案。

0 个答案:

没有答案