Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使用VB.net从进程数组中删除重复项_Arrays_Vb.net - Fatal编程技术网

Arrays 使用VB.net从进程数组中删除重复项

Arrays 使用VB.net从进程数组中删除重复项,arrays,vb.net,Arrays,Vb.net,我必须编写代码才能获得所有可访问的进程,但我需要删除此数组中的重复项,并且每个进程只显示一次 如何做到这一点是最好的方法,因为我认为进程数组不像普通数组 我的代码: For Each p As Process In Process.GetProcesses Try 'MsgBox(p.ProcessName + " " + p.StartTime.ToString) Catch ex As Exception 'Do nothing End

我必须编写代码才能获得所有可访问的进程,但我需要删除此数组中的重复项,并且每个进程只显示一次

如何做到这一点是最好的方法,因为我认为进程数组不像普通数组

我的代码:

For Each p As Process In Process.GetProcesses
    Try
        'MsgBox(p.ProcessName + " " + p.StartTime.ToString)
    Catch ex As Exception
        'Do nothing
    End Try
Next
提前感谢

该方法返回一个数组。您可以使用该方法,为其提供

例如比较器:

Public Class ProcessComparer
    Implements IEqualityComparer(Of Process)

    Public Function Equals1(p1 As Process, p2 As Process) As Boolean Implements IEqualityComparer(Of Process).Equals
        ' Check whether the compared objects reference the same data. 
        If p1 Is p2 Then Return True
        'Check whether any of the compared objects is null. 
        If p1 Is Nothing OrElse p2 Is Nothing Then Return False
        ' Check whether the Process' names are equal. 
        Return (p1.ProcessName = p2.ProcessName)
    End Function

    Public Function GetHashCode1(process As Process) As Integer Implements IEqualityComparer(Of Process).GetHashCode
        ' Check whether the object is null. 
        If process Is Nothing Then Return 0
        ' Get hash code for the Name field if it is not null. 
        Return process.ProcessName.GetHashCode()
    End Function
End Class
您可以这样使用它:

Sub Main()
    Dim processes As Process() = Process.GetProcesses()
    Console.WriteLine(String.Format("Count before Distinct = {0}", processes.Length))

    ' Should contain less items
    Dim disProcesses As Process() = processes.Distinct(New ProcessComparer()).ToArray()
    Console.WriteLine(String.Format("Count after Distinct = {0}", disProcesses.Length))

    Console.ReadLine()
End Sub
您可能需要根据您的规范和使用情况对比较器进行优化。

该方法返回一个数组。您可以使用该方法,为其提供

例如比较器:

Public Class ProcessComparer
    Implements IEqualityComparer(Of Process)

    Public Function Equals1(p1 As Process, p2 As Process) As Boolean Implements IEqualityComparer(Of Process).Equals
        ' Check whether the compared objects reference the same data. 
        If p1 Is p2 Then Return True
        'Check whether any of the compared objects is null. 
        If p1 Is Nothing OrElse p2 Is Nothing Then Return False
        ' Check whether the Process' names are equal. 
        Return (p1.ProcessName = p2.ProcessName)
    End Function

    Public Function GetHashCode1(process As Process) As Integer Implements IEqualityComparer(Of Process).GetHashCode
        ' Check whether the object is null. 
        If process Is Nothing Then Return 0
        ' Get hash code for the Name field if it is not null. 
        Return process.ProcessName.GetHashCode()
    End Function
End Class
您可以这样使用它:

Sub Main()
    Dim processes As Process() = Process.GetProcesses()
    Console.WriteLine(String.Format("Count before Distinct = {0}", processes.Length))

    ' Should contain less items
    Dim disProcesses As Process() = processes.Distinct(New ProcessComparer()).ToArray()
    Console.WriteLine(String.Format("Count after Distinct = {0}", disProcesses.Length))

    Console.ReadLine()
End Sub

您可能需要根据您的规格和使用情况对比较器进行优化。

为什么您认为它不像普通阵列?“Process类型的数组,表示本地计算机上运行的所有进程资源。”为什么您认为它与普通数组不同?“Process类型的数组,表示本地计算机上运行的所有进程资源。”