J'ai créé un formulaire de fenêtre simple (collé ci-dessous). Maintenant, je l'utilise pour afficher un dailog d'attente à l'utilisateur lors de l'exécution d'une opération complexe qui consomme du temps.Le formulaire Windows ne se ferme pas lorsqu'il est ouvert à partir d'un autre thread
L'appel du HerculesWaitForm.Show("Some Title","Some Caption...)
affiche le formulaire comme d'habitude mais lorsque vous appelez HerculesWaitForm.Close
affiche toujours le formulaire. Que puis-je faire pour surmonter le problème?
Imports System.Threading
Public Class HerculesWaitForm
Inherits DevExpress.Utils.WaitDialogForm
Private Shared form As HerculesWaitForm
Private Shared _thread As Thread
Private Shared _caption As String, _title As String
Private Sub New(ByVal caption As String, ByVal title As String)
MyBase.New(caption, title)
End Sub
Private Shared Sub CreateInstance()
form = New HerculesWaitForm(_caption, _title)
Application.Run(form)
Application.DoEvents()
End Sub
Public Overloads Shared Sub Show(ByVal caption As String, ByVal title As String)
_caption = caption
_title = title
If form Is Nothing Then
_thread = New Thread(AddressOf CreateInstance)
_thread.IsBackground = True
_thread.Start()
End If
End Sub
Public Overloads Shared Sub SetCaption(ByVal caption As String)
If form IsNot Nothing Then
_caption = caption
form.SetFormCaption()
Else
Show(caption, "")
End If
End Sub
Public Overloads Shared Sub Close()
If form IsNot Nothing Then
form.CloseForm()
form = Nothing
End If
End Sub
Private Sub CloseForm()
If Me.InvokeRequired Then
Invoke(New MethodInvoker(AddressOf CloseForm))
Return
End If
Application.ExitThread()
End Sub
Private Sub SetFormCaption()
If Me.InvokeRequired Then
Invoke(New MethodInvoker(AddressOf SetFormCaption))
Return
End If
MyBase.SetCaption(_caption)
End Sub
End Class
Avez-vous des exceptions à ce sujet? Consultez cette question, de toute façon: http://stackoverflow.com/questions/947476/calling-a-windows-form-from-another-thread-net –
Si l'opération se termine très vite, le problème persiste mais si le prend un certain temps puis il fonctionne comme prévu. –
Bunch de code, n'a pas réellement un appel Form.Close(). HerculesWaitForm est un * nom de type *, n'utilisez jamais HerculesWaitForm.Close(). Cela utilise le mauvais objet lorsque vous faites cela dans un fil. –