By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,678 Members | 1,144 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,678 IT Pros & Developers. It's quick & easy.

Is there a better way: Displaying a Working Dialog

P: n/a
Hello Everyone,

Just curious how far off the mark I am with my Working Dialog. Any
improvements are welcomed.

Scenario, we have an application (vb'05) that at known times performs tasks
that take some time to process. During those times, I would like to display
a dialog that informs the user that work is occuring.

We have a base form that all other forms derive from. It is in this form
that I put the logic for enabling and disabling the wait dialog.

Here is a simple sample use:
class form1
inherits baseForm

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

showDelay()

Me.Button1.Enabled = False
Dim aList As ArrayList = New ArrayList

For i As Integer = 1 To 10

For j As Integer = 1 To 10000
'Hurt the processor
aList.Add(i)
Application.DoEvents()
Next

Debug.Print(i.ToString)

Next

Me.Button1.Enabled = True

endDelay()

MsgBox("Job's Done!")

End Sub

end class

Here is my code

'--==Class baseForm.vb==--

Protected f As splashForm
Protected inDelay As Boolean

Private Sub showDelay()

If inDelay Then Exit Sub
inDelay = True

Dim imageFirst As System.Drawing.Bitmap
Dim imagesecond As System.Drawing.Bitmap
Dim myStream As System.IO.Stream
Dim thisExe As System.Reflection.Assembly

thisExe = System.Reflection.Assembly.GetEntryAssembly
myStream =
thisExe.GetManifestResourceStream(String.Format("{ 0}.imageFirst.bmp",
thisExe.GetName.Name))
imageFirst = System.Drawing.Bitmap.FromStream(myStream)
myStream =
thisExe.GetManifestResourceStream(String.Format("{ 0}.imageSecond.bmp",
thisExe.GetName.Name))
imagesecond = System.Drawing.Bitmap.FromStream(myStream)

f = New splashForm(imageFirst, imagesecond)

f.Show(Me)
f.Process(False)

End Sub

Private Sub endDelay()

If inDelay Then
f.Stop()
f.Dispose()
End If

inDelay = False

End Sub

'--===Class splashForm.vb==--

Public Class splashForm

Private EndDelay As Boolean
Private OnFirst As Boolean = True

Private imageFirst As System.Drawing.Bitmap
Private imageSecond As System.Drawing.Bitmap

Public Sub New(ByVal oimageFirst As System.Drawing.Bitmap, ByVal
oimageSecond As System.Drawing.Bitmap)

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
imageFirst = oimageFirst
imageSecond = oimageSecond

Me.BackgroundImage = imageFirst
EndDelay = False

End Sub

Public Sub [Stop]()
EndDelay = True
End Sub

Public Sub Process(Optional ByVal ShowProcessing As Boolean = True)

Dim worker As System.ComponentModel.BackgroundWorker

worker = New System.ComponentModel.BackgroundWorker
worker.WorkerReportsProgress = True
worker.WorkerSupportsCancellation = True

If ShowProcessing Then
Me.BackgroundImage = imageSecond
Else
Me.BackgroundImage = imageFirst
End If

AddHandler worker.ProgressChanged, AddressOf progressChanged
AddHandler worker.RunWorkerCompleted, AddressOf runWorkerCompleted
AddHandler worker.DoWork, AddressOf doWork

EndDelay = False

worker.RunWorkerAsync()

End Sub

Private Sub doWork(ByVal sender As Object, ByVal e As
System.ComponentModel.DoWorkEventArgs)

'Temp var used as a flag
Dim DontStop As Boolean

' Get the BackgroundWorker object that raised this event.
Dim worker As System.ComponentModel.BackgroundWorker = CType(sender,
System.ComponentModel.BackgroundWorker)

DontStop = True

Do While DontStop

SyncLock (Me)
DontStop = Not EndDelay
End SyncLock

'Trace.WriteLine(String.Format("Processing and dontstop = {0}",
DontStop.ToString))

worker.ReportProgress(0)

Threading.Thread.Sleep(50)

Loop

End Sub

Private Sub progressChanged(ByVal sender As Object, ByVal e As
System.ComponentModel.ProgressChangedEventArgs)

Static GoNorth As Boolean

If Not EndDelay Then

If Me.Opacity >= 1D Then GoNorth = False
If Me.Opacity <= 0.01D Then GoNorth = True

If GoNorth Then
Me.Opacity += 0.05D
Else
Me.Opacity -= 0.05D
End If

Me.CenterToParent()

End If

End Sub

Private Sub runWorkerCompleted(ByVal sender As Object, ByVal e As
System.ComponentModel.RunWorkerCompletedEventArgs)

Dim worker As System.ComponentModel.BackgroundWorker = CType(sender,
System.ComponentModel.BackgroundWorker)

RemoveHandler worker.ProgressChanged, AddressOf progressChanged
RemoveHandler worker.RunWorkerCompleted, AddressOf runWorkerCompleted
RemoveHandler worker.DoWork, AddressOf doWork

End Sub

End Class
Aug 11 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.