VB .NET代码错误?

问题描述:

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



上面的代码将停止在列表视图上选中的服务(复选框= true)
但是当我选择停止"按钮时,它将一次又一次地停止并停止服务...但是我只希望当我单击该按钮时,它只会停止一次服务..... (对不起,我的英语不好)



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)

这是因为您要遍历列表中的每个服务,以避免仅使用exit停止服务后的子.退出dd行Exit Sub
如果有任何问题,请问我.

谢谢,
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,


这里有很多需要解决的问题,发现通过解决其他一些问题可以解决问题也就不足为奇了.

首先,您正在吃掉所有例外.这不是一个好主意.如果您有一个Try Catch块,则应捕获该异常并对其进行处理.您将忽略抛出的任何异常.这是一个问题.如果您的代码确实有错误,您将不会知道,因为您永远不会看到异常.

接下来,您将在UI线程上完成所有工作,然后尝试使系统通过DoEvents进行响应.这不是一个好的做法,它可能导致一些无法预测的结果.要么处理锁定,要么将代码放在辅助线程上.如果您使用的是.NET 4.0,则任务并行"库确实使诸如此类的事情变得很容易.

最后,这是一个速度问题,而不是实际问题,您会在列表视图中获得针对每个项目的服务列表.如果列表视图中有10个项目,则您将调用GetServices十次.除非我缺少任何东西,否则这些服务在查看一行和下一行之间不会改变.当然,您会看到刚刚关闭的服务实际上已关闭,但这与列表中的下一项无关.将调用移至foreach循环外的GetServices.

完成所有这些操作后,再次运行代码,看看是否遇到异常.如果不是,并且仍然不起作用,请在代码中放置断点以查看实际发生的情况.如果您希望该服务停止一次,请确定其停止的位置并在该位置放置一个断点.然后观察接下来会发生什么,因为这是您发现问题的地方.
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.