pausing an application

R

RSH

I am writing a VB .Net App (Not ASP .Net) where I'm having an issue creating
a cancel button...


I have a situation where I have a loop that is initiated when the user
clicks on a Run button. I would like to have a Cancel button that when the
user clicks it a message button a messagebox will be displayed asking if
they would like to cancel or not...if they select yes I would like to break
out of the loop but not quit the application.

Basically I have it setup that a global variable called bRunApp = True and
when the button is clicked it changes the bRunApp value = False. in the
loop there is a check for the value;

if bRunApp = False Then
'display messagebox and breakout of the loop
EndIf

How would I do this?
 
H

Herfried K. Wagner [MVP]

RSH said:
I have a situation where I have a loop that is initiated when the user
clicks on a Run button. I would like to have a Cancel button that when
the user clicks it a message button a messagebox will be displayed asking
if they would like to cancel or not...if they select yes I would like to
break out of the loop but not quit the application.

Basically I have it setup that a global variable called bRunApp = True and
when the button is clicked it changes the bRunApp value = False. in the
loop there is a check for the value;

if bRunApp = False Then
'display messagebox and breakout of the loop
EndIf

What's the problem with the solution you describe? Don't forget to call
'Application.DoEvents' from time to time to prevent the UI from being
blocked.
 
C

Cor Ligthert

RSH,

You can only do this when you create an extra procedure for this

\\\
MyCancelButton.visible = true
Do while swMyCancelButton is not clicked
doMySub
application.doevents
loop
MyCancelButton.visible = false
///

I hope this explains as well the rest otherwise reply and otherswise I hope
it helps.

Cor
 
M

m.posseth

Hello RSH ??? :)


here is a complete example of how to do this copy the below code in a form


Imports System.Threading

Public Class Form1

Inherits System.Windows.Forms.Form

Private blnTrun As Boolean

Private thrTest As Thread

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents TStart As System.Windows.Forms.Button

Friend WithEvents TStop As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.TStart = New System.Windows.Forms.Button

Me.TStop = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'TStart

'

Me.TStart.Location = New System.Drawing.Point(16, 40)

Me.TStart.Name = "TStart"

Me.TStart.Size = New System.Drawing.Size(248, 32)

Me.TStart.TabIndex = 0

Me.TStart.Text = "Start Thread"

'

'TStop

'

Me.TStop.Location = New System.Drawing.Point(16, 96)

Me.TStop.Name = "TStop"

Me.TStop.Size = New System.Drawing.Size(248, 32)

Me.TStop.TabIndex = 1

Me.TStop.Text = "Stop Thread"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 273)

Me.Controls.Add(Me.TStop)

Me.Controls.Add(Me.TStart)

Me.Name = "Form1"

Me.Text = "Texampe"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub TStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TStart.Click

Try

blnTrun = True

thrTest = New Thread(AddressOf ThreadRun)

thrTest.Name = "thrTest"

thrTest.Start()

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub

Private Sub ThreadRun()

Dim iCount As Integer

Do Until Not blnTrun

Debug.WriteLine(String.Concat("thread running : ", iCount.ToString))

iCount += 1

Thread.Sleep(0)

Loop

End Sub

Private Sub TStop_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TStop.Click

Dim msg As String

Dim title As String

Dim style As MsgBoxStyle

Dim response As MsgBoxResult

msg = "Do you want to continue?"

style = MsgBoxStyle.DefaultButton2 Or _

MsgBoxStyle.Critical Or MsgBoxStyle.YesNo

title = "Thread Demonstration"

response = MsgBox(msg, style, title)

If response = MsgBoxResult.Yes Then ' User chose Yes. SO KEEP RUNNING

Else

blnTrun = False

' Perform some other action.

End If

End Sub

End Class

happy coding :)

Michel Posseth [MCP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top