我想在异步线程中打开一个带有ProgressRing的wpf窗口(Window1.xaml)。 当调用窗口构造函数时,出现异常:"调用线程无法访问此对象,因为另一个线程拥有该对象"。
窗户:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:metro="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d"
Title="Window1" Height="300" Width="300">
<Grid Name="myGrid">
<metro:ProgressRing Name="myring" ></metro:ProgressRing>
</Grid>
该线程由Attesa类运行:
Imports System.ServiceModel
Imports System.Threading
Imports System.Windows.Threading
Imports MahApps.Metro.Controls
Public Class Attesa
Implements IDisposable
Private _sender As Object
Private newDialog As DialogAttesa
Private test As Window1
Private oAvvia As Threading.Thread
Private Delegate Sub ChiudiFormDelegate()
Public Sub New()
Avvio()
End Sub
Private disposedValue As Boolean ' Per rilevare chiamate ridondanti
Public Property Sender As Object
Get
Return _sender
End Get
Set(value As Object)
_sender = value
End Set
End Property
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not disposedValue Then
If disposing Then
Chiusura()
End If
End If
disposedValue = True
End Sub
Protected Overrides Sub Finalize()
' Non modificare questo codice. Inserire il codice di pulizia in Dispose(ByVal disposing As Boolean).
Dispose(False)
MyBase.Finalize()
End Sub
' Questo codice è aggiunto da Visual Basic per implementare in modo corretto il modello Disposable.
Public Sub Dispose() Implements IDisposable.Dispose
' Non modificare questo codice. Inserire il codice di pulizia in Dispose(ByVal disposing As Boolean).
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Private Sub Avvio()
oAvvia = New Thread(AddressOf ApriForm)
oAvvia.SetApartmentState(ApartmentState.STA)
oAvvia.Start()
End Sub
Private Sub ApriForm()
test = New Window1()
test.ShowDialog()
End Sub
Private Sub Chiusura()
ChiudiForm()
End Sub
Private Sub ChiudiForm()
While test Is Nothing
End While
If test.Dispatcher.CheckAccess() Then
test.Close()
test = Nothing
Else
test.Dispatcher.Invoke(New ChiudiFormDelegate(AddressOf ChiudiForm))
End If
End Sub
结束班
当我在项目中使用该类时:
Using a As Attesa = New Attesa()
'something to do
End Using`
出现异常:&#34;调用线程无法访问此对象,因为另一个线程拥有该对象&#34;。
你能帮帮我吗? 感谢