How to add Cancel button to threaded 'Please Wait' Form (1 Viewer)

desmond

Registered User.
Local time
Today, 16:00
Joined
Dec 8, 2009
Messages
28
I am trying to build an application that exports a datagridview to excel. That bit's easy, but I wanted a 'please wait with animated gif' to display whilst it does. After a lot of trying different things, I eventually got that to work by wrapping the thread in a class. However, I would now like to add a cancel button to the please wait form, but I just can't get it to work. I don't know how to pass the message back to the class from the threaded form, and then from the class to the main form.

Form 1

Dim WaitDisplay As New WaitClass()
WaitDisplay.SetParent(Me)

Dim t As Thread
t = New Thread(AddressOf WaitDisplay.PleaseWait)
t.Start()

Export_Excel(DatagridView1)

t.abort()


Public Class WaitClass
Dim owner As Form1
Public Sub SetParent(ByVal parent As Form1)
owner = parent
End Sub
Public Sub PleaseWait()
Dim Please_Wait As New WaitScreen
Please_Wait.ShowDialog()
End Sub
End Class


Separate Form - just has text that says please wait with an animated gif.

Public NotInheritable Class WaitScreen
Private Sub WaitScreen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class

Can anyone help?
 

DJkarl

Registered User.
Local time
Today, 01:00
Joined
Mar 16, 2007
Messages
1,028
Create a Cancel event in your WaitClass. Then using your class WaitDisplay.Canceled assign that to a function that aborts the thread, or closes the form, or whatever you want to do in the main thread.

Air Code...Untested, this is just an example of how you might setup a Cancel event.
Code:
Private b As Button  

Public Delegate Sub CancelEvent() 
Public Event Canceled As CancelEvent 

Private Sub t()     
 b.Click += New EventHandler(AddressOf b_Click) 
End Sub  
Private Sub b_Click(sender As Object, e As EventArgs)     
  RaiseEvent Canceled() 
End Sub
 

Users who are viewing this thread

Top Bottom