我的一个Datagrid位于我的一个视图上,绑定到一个名为“Installed Applications”的集合,我创建了大约1000个以下任务。每次我从任务中添加到集合时,我都会收到stackoverflow异常,无法调试或查看异常数据。我迷失了,并尝试了各种不同的方式来完成我想要做的事情。问题是我有另一个函数基本上做了完全相同的事情,但没有抛出异常。
非工作代码
Private Sub ViewInstalledApplications(ByVal rec As MuellerAssetModel)
Dim t As Task = MuellerWMIFactory.CreateNewTaskFactoryJob _
(Sub()
If _taskCancellationToken.IsCancellationRequested Then
_taskCancellationToken.ThrowIfCancellationRequested()
End If
If My.Computer.Network.Ping(rec.IPAddress) Then
Dim searcherScope As New ManagementScope("\\" & rec.AssetName & "\root\cimv2")
Dim query As New ObjectQuery("SELECT * FROM Win32_Product")
Dim connectionOptions As New System.Management.ConnectionOptions
connectionOptions.Authentication = AuthenticationLevel.Call
connectionOptions.Username = MuellerWMICrypto.USER
connectionOptions.SecurePassword = MuellerWMICrypto.PWD
connectionOptions.Impersonation = ImpersonationLevel.Impersonate
searcherScope.Options = connectionOptions
Using mosSearcher As New ManagementObjectSearcher(searcherScope, query)
If _taskCancellationToken.IsCancellationRequested Then
_taskCancellationToken.ThrowIfCancellationRequested()
End If
Try
If _taskCancellationToken.IsCancellationRequested Then
_taskCancellationToken.ThrowIfCancellationRequested()
End If
For Each prd As ManagementObject In mosSearcher.Get
Dim prod As ROOT.CIMV2.Product = New ROOT.CIMV2.Product(prd)
Dim mProd As New AssetProduct With _
{.AssetLocation = rec.IPLocation, _
.AssetName = rec.AssetName, _
.Domain = rec.Domain, _
.InstallLocation = prod.InstallLocation, _
.InstallSource = prod.InstallSource, _
.LocalPackage = prod.LocalPackage, _
.ProductDescription = prod.Description, _
.ProductName = prod.Name, _
.ProductPackageName = prod.PackageName, _
.StatusMessage = "Call Success", _
.WMICallSuccess = True, _
.SystemType = rec.SystemType, _
.SystemTypeCode = rec.SystemTypeCode}
Application.Current.Dispatcher.BeginInvoke(Sub()
InstalledApplications.Add(mProd)
End Sub)
Next
_installedAppsCount += 1
Application.Current.Dispatcher.BeginInvoke(Sub()
InstalledApplicationProgress = (_installedAppsCount / _installedAppsTotalCount) * 100
End Sub)
Catch ex As Exception
If _taskCancellationToken.IsCancellationRequested Then
_taskCancellationToken.ThrowIfCancellationRequested()
End If
Dim mProd As New AssetProduct With _
{.AssetLocation = rec.IPLocation, _
.AssetName = rec.AssetName, _
.Domain = rec.Domain, _
.StatusMessage = ex.Message, _
.WMICallSuccess = True, _
.SystemType = rec.SystemType, _
.SystemTypeCode = rec.SystemTypeCode}
Application.Current.Dispatcher.BeginInvoke(Sub()
InstalledApplications.Add(mProd)
End Sub)
_installedAppsCount += 1
Application.Current.Dispatcher.BeginInvoke(Sub()
InstalledApplicationProgress = (_installedAppsCount / _installedAppsTotalCount) * 100
End Sub)
End Try
End Using
Else
If _taskCancellationToken.IsCancellationRequested Then
_taskCancellationToken.ThrowIfCancellationRequested()
End If
Dim mProd As New AssetProduct With _
{.AssetLocation = rec.IPLocation, _
.AssetName = rec.AssetName, _
.Domain = rec.Domain, _
.StatusMessage = "Failed Network Ping", _
.WMICallSuccess = True, _
.SystemType = rec.SystemType, _
.SystemTypeCode = rec.SystemTypeCode}
Application.Current.Dispatcher.BeginInvoke(Sub()
InstalledApplications.Add(mProd)
End Sub)
_installedAppsCount += 1
Application.Current.Dispatcher.BeginInvoke(Sub()
InstalledApplicationProgress = (_installedAppsCount / _installedAppsTotalCount) * 100
End Sub)
End If
End Sub, _taskCancellationTokenSource.Token)
_myTasks.Add(t)
End Sub
工作代码不同但主要相同
Private Sub ViewAssetRunningApplications(ByVal rec As MuellerAssetModel)
Dim t As Task = MuellerWMIFactory.CreateNewTaskFactoryJob _
(Sub()
If _taskCancellationToken.IsCancellationRequested Then
_taskCancellationToken.ThrowIfCancellationRequested()
End If
If My.Computer.Network.Ping(rec.IPAddress) Then
Dim searcherScope As New ManagementScope("\\" & rec.AssetName & "\root\cimv2")
Dim query As New ObjectQuery("SELECT * FROM Win32_Process")
Dim connectionOptions As New System.Management.ConnectionOptions
connectionOptions.Authentication = AuthenticationLevel.Call
connectionOptions.Username = MuellerWMICrypto.USER
connectionOptions.SecurePassword = MuellerWMICrypto.PWD
connectionOptions.Impersonation = ImpersonationLevel.Impersonate
searcherScope.Options = connectionOptions
Using mosSearcher As New ManagementObjectSearcher(searcherScope, query)
If _taskCancellationToken.IsCancellationRequested Then
_taskCancellationToken.ThrowIfCancellationRequested()
End If
Try
If _taskCancellationToken.IsCancellationRequested Then
_taskCancellationToken.ThrowIfCancellationRequested()
End If
For Each prc As ManagementObject In mosSearcher.Get
Dim proc As ROOT.CIMV2.Process = New ROOT.CIMV2.Process(prc)
Dim ap As New AssetProcess With _
{.AssetLocation = rec.IPLocation, _
.AssetName = rec.AssetName, _
.AssetProcessNameExec = proc.Name, _
.AssetProcessPath = proc.ExecutablePath, _
.Domain = rec.Domain, _
.SystemType = rec.SystemType, _
.SystemTypeCode = rec.SystemTypeCode, _
.StatusMessage = "Call Success", _
.WMICallSuccess = True}
ap.ProcessID = proc.ProcessId
ap.PageFaults = proc.PageFaults
ap.PageFileSize = proc.PageFileUsage
ap.WorkingSetSize = proc.WorkingSetSize
ap.ProcessorTime = (CSng(proc.KernelModeTime) + CSng(proc.UserModeTime) / 10000000)
ap.Description = proc.Description
ap.Caption = proc.Caption
If Not proc.IsInstallDateNull = True Then
ap.InstallDate = proc.InstallDate
End If
ap.ProcessStatus = proc.Status
proc.GetOwner(ap.UserDomain, ap.User)
If ap.User = "SYSTEM" Or _
ap.User = "LOCAL SYSTEM" Or _
ap.User = "NETWORK SERVICE" Or _
ap.User = "LOCAL SERVICE" Then
Else
Application.Current.Dispatcher.BeginInvoke(Sub()
RunningApplications.Add(ap)
End Sub)
End If
Next
Application.Current.Dispatcher.BeginInvoke(Sub()
RunningApplicationsProgress = (_runningAppsCount / _runningAppsTotalCount) * 100
End Sub)
_runningAppsCount += 1
Catch ex As Exception
If _taskCancellationToken.IsCancellationRequested Then
_taskCancellationToken.ThrowIfCancellationRequested()
End If
Dim ap As New AssetProcess With _
{.AssetLocation = rec.IPLocation, _
.AssetName = rec.AssetName, _
.AssetProcessNameExec = "Error", _
.AssetProcessPath = "Error", _
.Domain = rec.Domain, _
.StatusMessage = ex.Message, _
.WMICallSuccess = True, .SystemType = rec.SystemType, .SystemTypeCode = rec.SystemTypeCode}
Application.Current.Dispatcher.BeginInvoke(Sub()
RunningApplications.Add(ap)
End Sub)
_runningAppsCount += 1
Application.Current.Dispatcher.BeginInvoke(Sub()
RunningApplicationsProgress = (_runningAppsCount / _runningAppsTotalCount) * 100
End Sub)
End Try
End Using
Else
If _taskCancellationToken.IsCancellationRequested Then
_taskCancellationToken.ThrowIfCancellationRequested()
End If
Dim ap As New AssetProcess With _
{.AssetLocation = rec.IPLocation, _
.AssetName = rec.AssetName, _
.AssetProcessNameExec = "Error", _
.AssetProcessPath = "Error", _
.Domain = rec.Domain, _
.StatusMessage = "Failed Network Ping", _
.WMICallSuccess = True, .SystemType = rec.SystemType, .SystemTypeCode = rec.SystemTypeCode}
Application.Current.Dispatcher.BeginInvoke(Sub()
RunningApplications.Add(ap)
End Sub)
_runningAppsCount += 1
Application.Current.Dispatcher.BeginInvoke(Sub()
RunningApplicationsProgress = (_runningAppsCount / _runningAppsTotalCount) * 100
End Sub)
End If
End Sub, _taskCancellationTokenSource.Token)
_myTasks.Add(t)
End Sub