BExplorer.Shell.ShellView.NavigateSearch C# (CSharp) Method

NavigateSearch() private method

private NavigateSearch ( IListItemEx destination, Boolean isInSameTab = false, Boolean refresh = false, Boolean isCancel = false ) : void
destination IListItemEx
isInSameTab Boolean
refresh Boolean
isCancel Boolean
return void
    private void NavigateSearch(IListItemEx destination, Boolean isInSameTab = false, Boolean refresh = false, Boolean isCancel = false) {
      SaveSettingsToDatabase(this.CurrentFolder);
      if (destination == null) return;
      if (this.RequestedCurrentLocation == destination && !refresh) return;
      //if (this._RequestedCurrentLocation != destination) {
      //}

      _ResetEvent.Set();

      if (this._Threads.Any()) {
        _Mre.Set();
        this._ResetEvent.Set();
        foreach (var thread in this._Threads.ToArray()) {
          thread.Abort();
          this._Threads.Remove(thread);
        }
      }

      this._UnvalidateTimer.Stop();
      this._IsDisplayEmptyText = false;
      User32.SendMessage(this.LVHandle, MSG.LVM_SETITEMCOUNT, 0, 0);
      this.DisableGroups();

      this._ItemForRename = -1;
      this._LastItemForRename = -1;

      Items.Clear();
      this._AddedItems.Clear();
      this.LargeImageList.ReInitQueues();
      this.SmallImageList.ReInitQueues();
      this._CuttedIndexes.Clear();
      this.RequestedCurrentLocation = destination;
      if (!refresh)
        Navigating?.Invoke(this, new NavigatingEventArgs(destination, isInSameTab));

      var columns = new Collumns();
      Int32 CurrentI = 0, LastI = 0, K = 0;
      this.IsNavigationInProgress = true;

      _ResetTimer.Stop();

      this.RemoveAllCollumns();
      this.AddDefaultColumns(false, true);

      this.IsViewSelectionAllowed = true;
      this.Invoke((Action)(() => this._NavWaitTimer.Start()));
      var navigationThread = new Thread(() => {
        destination = FileSystemListItem.ToFileSystemItem(destination.ParentHandle, destination.PIDL);
        this.RequestedCurrentLocation = destination;
        this.Invoke((Action)(() => {
          if (!this._SearchTimer.Enabled) {
            this._SearchTimer.Start();
          }
        }));

        foreach (var shellItem in destination.TakeWhile(shellItem => !this.IsCancelRequested)) {
          CurrentI++;
          _Smre.WaitOne();

          if (this.ShowHidden || !shellItem.IsHidden) {
            shellItem.ItemIndex = K++;
            this.Items.Add(shellItem);
            if (CurrentI == 1) {
              this.Invoke((Action)(() => {
                this._NavWaitTimer.Stop();
                this._IsDisplayEmptyText = false;
                this._IIListView.ResetEmptyText();
              }));
            }
          }

          var delta = CurrentI - LastI;
          if (delta >= (this.IsSearchNavigating ? 1 : 5000)) {
            LastI = CurrentI;
          }
          if (this.IsSearchNavigating && delta >= 20)
            Shell32.SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
        }
        this.IsCancelRequested = false;
        this.IsNavigationInProgress = false;

        if (this.RequestedCurrentLocation.NavigationStatus != HResult.S_OK) {
          this.Invoke((Action)(() => {
            if (this._SearchTimer.Enabled)
              this._SearchTimer.Stop();
          }));
          this.BeginInvoke((Action)(() => {
            var navArgs = new NavigatedEventArgs(this.RequestedCurrentLocation, this.CurrentFolder, isInSameTab);
            this.CurrentFolder = this.RequestedCurrentLocation;

            if (!refresh)
              Navigated?.Invoke(this, navArgs);
          }));

          GC.Collect();
          Shell32.SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
          if (this._Threads.Count <= 1) return;
          _Mre.Set();
          this._ResetEvent.Set();
          this._Threads[0].Abort();
          this._Threads.RemoveAt(0);
          return;
        }

        var headerhandle = User32.SendMessage(this.LVHandle, MSG.LVM_GETHEADER, 0, 0);
        for (var i = 0; i < this.Collumns.Count; i++) {
          this.Collumns[i].SetSplitButton(headerhandle, i);
        }

        if (this.View != ShellViewStyle.Details) AutosizeAllColumns(-2);

        var sortColIndex = 0;
        if (sortColIndex > -1) this.SetSortIcon(sortColIndex, SortOrder.Ascending);

        this.SetSortCollumn(false, this.Collumns.First(), SortOrder.Ascending, false);

        this.BeginInvoke((Action)(() => {
          var navArgs = new NavigatedEventArgs(this.RequestedCurrentLocation, this.CurrentFolder, isInSameTab);
          this.CurrentFolder = this.RequestedCurrentLocation;
          if (!refresh)
            Navigated?.Invoke(this, navArgs);
        }));

        GC.Collect();
        Shell32.SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
        this.Invoke((Action)(() => {
          if (this._SearchTimer.Enabled)
            this._SearchTimer.Stop();
        }));
        _Mre.Reset();
        _Mre.WaitOne();
      });
      navigationThread.SetApartmentState(ApartmentState.STA);
      this._Threads.Add(navigationThread);
      navigationThread.Start();
    }
ShellView