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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RaiseEvent - Event running multiple times 1

Status
Not open for further replies.

AnonGod

IS-IT--Management
Jun 5, 2000
223
This app restarts services on a server. Everything is working well except the sub that runs when the custom event is fired is running multiple times. Example - first time btnTest is clicked, the event code runs once. Second time the btnTest is clicked, the event code runs twice, and so on.

Thought I might have missed something in the event handling like clearing a buffer or queue, I don't know. Any help is appreciated. Thanks!

:) -Andrew

Code:
Public Class Services_Class
	
    ' Public Events
    Public Event RestartComplete(ByVal success As Boolean)
	
    Public Sub RestartServices()
        MsgBox(location)
		
        If MsgBox("Are you sure?", MsgBoxStyle.YesNo, "Confirm") <> MsgBoxResult.Yes Then
            RaiseEvent RestartComplete(False)
            Exit Sub
        End If

        ErrorMsg("Test Error Message")
        RaiseEvent RestartComplete(True)
        Exit Sub
    End Sub
End Class

' Form Code:
	Dim cServices As New Services_Class

	Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        ' Restart
        btnTest.Enabled = False
        Dim thread1 As New Threading.Thread(AddressOf cServices.RestartServices)
        AddHandler cServices.RestartComplete, AddressOf RestartCompleteEventHandler
        StatusBar2.Panels(0).Text = "Please wait... restarting services..."
        thread1.Start()
    End Sub
    
    Private Sub RestartCompleteEventHandler(ByVal success As Boolean)
        MsgBox("RestartComplete: " & success.ToString)
        If success Then
            StatusBar2.Panels(0).Text = "Services successfully restarted."
            MsgBox("Services restarted.")
        Else
            StatusBar2.Panels(0).Text = "There was an error restarting services."
        End If

        btnTest.Enabled = True
    End Sub

alien.gif

[Signature modified by admin request]
-TAG
anongod@hotmail.com
'Drawing on my fine command of language, I said nothing.'
 
Forgot to add:

Adding a break point and stepping through the process shows everything running as it should the first time. The event gets fired and the event handler runs once. The second time, after the event handler sub runs once, it restarts back at the top of the event handler and runs a second time. Almost like the event was fired twice and it is just catching up. 3rd time it runs 3 times etc...

Thanks!
:) -Andrew

alien.gif

[Signature modified by admin request]
-TAG
anongod@hotmail.com
'Drawing on my fine command of language, I said nothing.'
 
Thanks Rick -

I'm sorry I don't fully understand. The event handler sub doesn't know what 'e' is. Looking up the handled property I found KeyEvent classes but nothing stuck out as something I would want. System.EventArgs.Handled doesn't seem to work either.

I'm sure the answer is right in front of me, it's just something new and I don't understand it yet.

Can you explain a little further what 'e' refers to and where it's coming from?

Thanks!
:) -Andrew

alien.gif

[Signature modified by admin request]
-TAG
anongod@hotmail.com
'Drawing on my fine command of language, I said nothing.'
 
I've created a class that will do all the restarting/monitoring/etc... that I want done. I want other form functions available to the user while services are being refreshed/restarted. If I want something done in the background while the user can still interact with the form I need seperate threads right?

So when the background thread is complete it fires the "I'm done" event. This is picked up by the form and alerts the user.

That right? I'm open to any and all help/suggestions.

Thanks!
:) -Andrew

alien.gif

[Signature modified by admin request]
-TAG
anongod@hotmail.com
'Drawing on my fine command of language, I said nothing.'
 
probably because you are adding another handler each time you click.

try puting this somewhereelse, in the constructor perhaps.

AddHandler cServices.RestartComplete, AddressOf RestartCompleteEventHandler


Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
That was it, makes perfect sense now that I see that.

Thanks so much!
:) -Andrew

alien.gif

[Signature modified by admin request]
-TAG
anongod@hotmail.com
'Drawing on my fine command of language, I said nothing.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top