11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33
4+ using Microsoft . PowerShell . PowerShellGet . UtilClasses ;
45using System ;
56using System . Collections . Generic ;
6- using Dbg = System . Diagnostics . Debug ;
77using System . Linq ;
88using System . Management . Automation ;
99using System . Threading ;
10- using Microsoft . PowerShell . PowerShellGet . UtilClasses ;
10+
11+ using Dbg = System . Diagnostics . Debug ;
1112
1213namespace Microsoft . PowerShell . PowerShellGet . Cmdlets
1314{
@@ -29,8 +30,8 @@ public sealed class FindPSResource : PSCmdlet
2930 private const string ResourceNameParameterSet = "ResourceNameParameterSet" ;
3031 private const string CommandNameParameterSet = "CommandNameParameterSet" ;
3132 private const string DscResourceNameParameterSet = "DscResourceNameParameterSet" ;
32- private CancellationTokenSource _source ;
33- private CancellationToken _cancellationToken ;
33+ private CancellationTokenSource _cancellationTokenSource ;
34+ private FindHelper _findHelper ;
3435
3536 #endregion
3637
@@ -132,17 +133,25 @@ public sealed class FindPSResource : PSCmdlet
132133
133134 protected override void BeginProcessing ( )
134135 {
135- _source = new CancellationTokenSource ( ) ;
136- _cancellationToken = _source . Token ;
136+ _cancellationTokenSource = new CancellationTokenSource ( ) ;
137+ _findHelper = new FindHelper (
138+ cancellationToken : _cancellationTokenSource . Token ,
139+ cmdletPassedIn : this ) ;
137140
138- // Create a respository story (the PSResourceRepository.xml file) if it does not already exist
141+ // Create a repository story (the PSResourceRepository.xml file) if it does not already exist
139142 // This is to create a better experience for those who have just installed v3 and want to get up and running quickly
140143 RepositorySettings . CheckRepositoryStore ( ) ;
141144 }
142145
143146 protected override void StopProcessing ( )
144147 {
145- _source . Cancel ( ) ;
148+ _cancellationTokenSource ? . Cancel ( ) ;
149+ }
150+
151+ protected override void EndProcessing ( )
152+ {
153+ _cancellationTokenSource . Dispose ( ) ;
154+ _cancellationTokenSource = null ;
146155 }
147156
148157 protected override void ProcessRecord ( )
@@ -207,10 +216,9 @@ private void ProcessResourceNameParameterSet()
207216 return ;
208217 }
209218
210- FindHelper findHelper = new FindHelper ( _cancellationToken , this ) ;
211219 List < PSResourceInfo > foundPackages = new List < PSResourceInfo > ( ) ;
212220
213- foreach ( PSResourceInfo package in findHelper . FindByResourceName (
221+ foreach ( PSResourceInfo package in _findHelper . FindByResourceName (
214222 Name ,
215223 Type ,
216224 Version ,
@@ -283,10 +291,9 @@ private void ProcessCommandOrDscParameterSet(bool isSearchingForCommands)
283291 moduleNamesToSearch = new string [ ] { "*" } ;
284292 }
285293
286- FindHelper findHelper = new FindHelper ( _cancellationToken , this ) ;
287294 List < PSResourceInfo > foundPackages = new List < PSResourceInfo > ( ) ;
288295
289- foreach ( PSResourceInfo package in findHelper . FindByResourceName (
296+ foreach ( PSResourceInfo package in _findHelper . FindByResourceName (
290297 name : moduleNamesToSearch ,
291298 // provide type so Scripts endpoint for PSGallery won't be searched
292299 type : isSearchingForCommands ? ResourceType . Command : ResourceType . DscResource ,
0 commit comments