Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't exit the program !!!

Status
Not open for further replies.

dgeva

Programmer
Oct 23, 2002
12
IL
Hi,
In the following program 2 clicks on Button1 are needed to get his attention (even minutes apart). The first click is always ignored.
Any Idea why?
____________________________________
Imports System.IO
Imports System.Windows.Forms.Application

Public Class Form1
Inherits System.Windows.Forms.Form

#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 TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(80, 72)
Me.TextBox1.Name = &quot;TextBox1&quot;
Me.TextBox1.Size = New System.Drawing.Size(104, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = &quot;&quot;
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(80, 144)
Me.Button1.Name = &quot;Button1&quot;
Me.Button1.Size = New System.Drawing.Size(104, 40)
Me.Button1.TabIndex = 1
Me.Button1.Text = &quot;EXIT&quot;
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(80, 104)
Me.Button2.Name = &quot;Button2&quot;
Me.Button2.Size = New System.Drawing.Size(104, 32)
Me.Button2.TabIndex = 2
Me.Button2.Text = &quot;START&quot;
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(264, 318)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.Button1, Me.TextBox1})
Me.Name = &quot;Form1&quot;
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

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

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim i, j As Long
Dim sum As Double
For i = 0 To 10000000
DoEvents()
TextBox1.Text = i
TextBox1.Refresh()
For j = 0 To 10000000
sum = 1.0 / j
Next
DoEvents()
Next

End Sub
End Class
 
Somtines the End command does not work very well, try Application.Exit() enstead.

Becca
 
Rebecca,
Thanks for your reply but the issue is that after the first click End or Exit() do not get a chance yet to run. Only after the second click the Button1 sub gets the CPU.
What I found in the mean time is that the first click moves the Focus to Botton1, so is I do Button1.focus in Button2 sub it will work in one click. This is a work arround which is no good if you have more than 2 controls.
Dan
 
I have never heard of such a problem. never had it myself either. if you run the program, and hit button1 do you still have to hit it twice, or just after you hit something else first?
 
As you can see I don't have anything else on the form (beside the TextBox). Yes I have to click Button1 twice.
Can you try the progran and let me know.
Thanks
Dan
 
Worked first time, every time for me (both Debug and Release configs) under Win2K and .NET Framework 1.1.
 
I have tested and retested the program, debug and release configs, on two machines, and it always needs two clicks.

I'm using XP pro and .NET Framework 1.0.

Would you agree that I'll mail you the 10k exe file to test on your machine.
 
Oops. I'm very sorry, I didn't really read the postings before blasting ahead and clicking the Exit button - before I had clicked the Start button!

What can I say ? It's been a long day...

The DoEvents does yield execution to allow other messages to get processed, but can cause problems, and in your case seems to be doing exactly that. I can only suggest that a seperate thread is the best way to go:
Code:
    Private th As Thread

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        th = New Thread(AddressOf TextBoxLoop)

        th.Start()

    End Sub

    Private Sub TextBoxLoop()

        Dim i, j As Long
        Dim sum As Double
        For i = 0 To 10
            'DoEvents()
            TextBox1.Text = i.ToString()
            'TextBox1.Refresh()
            For j = 0 To 10000000
                sum = 1.0 / j
            Next
            'DoEvents()
        Next

        th.Abort()

    End Sub
This code was quick and dirty - you will need to include some proper error handling.

Sorry again for not reading the original posts properly...
 
SHelton,
Thanks for your clear answer, I only wonder, the case presented in my program is extremely simple yet DoEvents failed. Is it VB.NET problem? I don't think this problem exists in VB6!
Dan
 
dgeva, it must be the .NET implementation of DoEvents - the VB6 version works on the first click. Maybe the .NET framework processes messages differently to the VB6 runtime.
 
SHeltom
Thanks for your help. Is there a way to draw the attention of Microsoft to this issue?
Dan
 
You have two huge looks. DoEvents is encasulated in one but not the other.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top