Click here to Skip to main content
15,884,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
VB
Dim flag As Boolean = False
 Dim item As ListViewItem
 For Each item In Me.ListViewServices.CheckedItems
     Application.DoEvents()
     Try
         Dim services As ServiceController() = ServiceController.GetServices
         Dim i As Integer
         For i = 0 To services.Length - 1
             Try
                 Do While (item.Text = services(i).ServiceName.Trim)
                     Application.DoEvents()
                     If (services(i).Status.ToString = "Stopped") Then
                         MessageBox.Show("The service(s) status stopped", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                         Me.TmbRefResh_Click(sender, e)
                         Application.DoEvents()
                     ElseIf services(i).CanStop Then
                         services(i).Stop()
                         Thread.Sleep(&H3E8)
                         item.Remove()
                         Application.DoEvents()
                     Else
                         MessageBox.Show("The service(s) can't be stop", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                     End If
                 Loop
             Catch obj1 As Exception
             End Try
             services(i).Refresh()
         Next i
         flag = True
     Catch obj2 As Exception
     End Try
 Next
 If Not flag Then
     MessageBox.Show("Please select service(s) do you want to end", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
     Me.Refresh()
 End If


The above code will stop the selected service(s) on a listview(checkbox = true)
but when I select the 'stop' button it will stop and stop the services again and again... but I only want when I click the button it will just stop the service(s) one time only...... (Sorry for my bad english)
Posted

It is because you are looping through every service in the list to avoid just exit the sub after you stop the service. to exit just dd the line Exit Sub
If any problem comes ask me.

Thanks,
 
Share this answer
 
Comments
Tim Corey 1-Jun-12 8:26am    
I'm not sure that is it. The sub deals with multiple items that are checked, which is why there isn't an End Sub after the service is stopped. The OP could probably go to the next item in the For Each, but even so I don't see how that will solve much beyond making the code work better. You won't find the same service in the list again so it isn't like that service would be turned off again that way, right?
Jαved 1-Jun-12 9:17am    
Oh yes sorry. thanks Tim.
There is a lot here that needs to be addressed and it wouldn't surprise to find out that the problem is solved by solving some of these other problems.

First, you are eating all of your exceptions. Not a good idea. If you have a Try Catch block, you should catch the exception and do something with it. You are ignoring any exceptions that are thrown. This is an issue. If your code does have a bug, you wouldn't know about it because you would never see the exceptions.

Next, you are doing all of your work on the UI thread and then trying to make the system responsive with DoEvents. Not a good practice and it can lead to some unpredictable results. Either deal with the locking or put your code on a worker thread. If you are using .NET 4.0, the Task Parallel library really makes things like this easy.

Finally, and this is a speed issue more than a real problem, you are getting a list of your services for every item in your list view. If you have 10 items in your list view, you are calling the GetServices ten times. Unless I'm missing somthing, those services won't change between looking at one row and the next. Sure, you will see that the service you just closed was actually closed, but that isn't relevant to the next item in the list. Move the call to GetServices outside the foreach loop.

Once you have done all of that, run your code again and see if you get an exception. If not, and if it still does not work, put breakpoints in your code to see what exactly is happening. If you are expecting the service to stop once, identify the place where it stops and put a breakpoint there. Then watch what happens next, because that is where you will find the problem.
 
Share this answer
 
Comments
ledtech3 3-Jun-12 4:43am    
What is this part here in the middle
" Me.TmbRefResh_Click(sender, e)
Application.DoEvents()"
Does that look like it keeps refreshing itself.
and what events does Application.DoEvents(), Do ? It calls it several times.
Jusy my opinion but I would think it would be better to get a list of the Checked items and Like Tim Corey said do a for each on them instead of the nested nightmare.
Update:
I never used a Do events so I had to Look it up.
http://msdn.microsoft.com/en-us/library/aa262728(v=VS.60).aspx and here

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents.aspx
Looks like it works differently between VB 6 and .Net 4.0
Tim Corey 4-Jun-12 10:05am    
Basically, DoEvents is a hack that tries to let the UI perform actions before the code locks the screen back up. It was used in VB6 to make the UI look like it was still working when a lot of work was being done on the UI thread.
ledtech3 4-Jun-12 10:10am    
What I have Seen so far in the reading, It looks like it can be very troublesome, also I think one of those links I posted actually suggest Not to use it.
Tim Corey 4-Jun-12 10:15am    
Yes, you should avoid using the DoEvents method if at all possible (an I have yet to need it in .NET since the beta for the first version of .NET came out). The reason you still see it is because typically programmers go from VB6 to VB.NET and they bring their habits along with them. Since VB.NET has a DoEvents method, they use it like they did in VB6. It is just one of those natural learning experiences that a programmer has to go through as they develop and learn .NET.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900