Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 操作时等待光标_Vb.net - Fatal编程技术网

Vb.net 操作时等待光标

Vb.net 操作时等待光标,vb.net,Vb.net,我有以下问题: 序列化需要一些时间,我想在这段时间内将游标更改为WaitCursor,以便用户知道程序仍在工作,并且没有崩溃。 但在序列化时,程序似乎会自动将光标更改为默认值。所以在这种情况下,我根本看不到WaitCursor。如果我删除Me.Cursor=Cursors.DefaultWaitCursor,则在保存新文件后会立即显示 有没有办法解决这个问题,并在程序运行时显示WaitCursor If Dialog.ShowDialog = Windows.Forms.DialogResult

我有以下问题:

序列化需要一些时间,我想在这段时间内将游标更改为WaitCursor,以便用户知道程序仍在工作,并且没有崩溃。 但在序列化时,程序似乎会自动将光标更改为默认值。所以在这种情况下,我根本看不到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。
因此,我认为可能有一个更简单的解决方案。

问题可能是您正在UI线程上完成所有工作,因此在此期间无法更新UI。一个快速而肮脏的解决方法可能是在设置
光标后调用表单上的
刷新
,但更好的解决方案是异步执行工作。通过调用
线程.Join()
可以让它等待
线程
完成,然后再执行其余代码,因此,您使它同步,并失去了拥有另一个线程的整个概念。删除
thread.Join()
,您应该可以开始了。但是程序如何知道另一个线程何时完成,光标何时可以设置回默认值?问题可能是您正在UI线程上完成所有工作,因此在此期间无法更新UI。一个快速而肮脏的解决方法可能是在设置
光标后调用表单上的
刷新
,但更好的解决方案是异步执行工作。通过调用
线程.Join()
可以让它等待
线程
完成,然后再执行其余代码,因此,您使它同步,并失去了拥有另一个线程的整个概念。删除
thread.Join()
,您就可以开始了。但是程序如何知道另一个线程何时完成,光标何时可以设置回默认值?