0

Relevant code:

private static Thread m_thread = null;
private static Boolean m_stop = false;

public static Boolean Start(SearcherParams pars)
{
    Boolean success = false;

    if (m_thread == null)
    {
        // Perform a reset of all variables,
        // to ensure that the state of the searcher is the same on every new start:
        ResetVariables();

        // Remember the parameters:
        m_pars = pars;

        // Start searching for FileSystemInfos that match the parameters:
        m_thread = new Thread(new ThreadStart(SearchThread));
        m_thread.Start();

        success = true;
    }

    return success;
}

private static void SearchThread()
{
    Boolean success = true;
    String errorMsg = "";

    // Search for FileSystemInfos that match the parameters:
    if ((m_pars.SearchDir.Length >= 3) && (Directory.Exists(m_pars.SearchDir)))
    {
        if (m_pars.FileNames.Count > 0)
        {
            // Convert the string to search for into bytes if necessary:
            if (m_pars.ContainingChecked)
            {
                if (m_pars.ContainingText != "")
                {
                    try
                    {
                        m_containingBytes = 
                            m_pars.Encoding.GetBytes(m_pars.ContainingText);
                    }
                    catch (Exception)
                    {
                        success = false;
                        errorMsg = "The string\r\n" + m_pars.ContainingText +
                                    "\r\ncannot be converted into bytes.";
                    }
                }
                else
                {
                    success = false;
                    errorMsg = "The string to search for must not be empty.";
                }
            }

            if (success)
            {
                // Get the directory info for the search directory:
                DirectoryInfo dirInfo = null;

                try
                {
                    dirInfo = new DirectoryInfo(m_pars.SearchDir);
                }
                catch (Exception ex)
                {
                    success = false;
                    errorMsg = ex.Message;
                }

                if (success)
                {
                    // Search the directory (maybe recursively),
                    // and raise events if something was found:
                    SearchDirectory(dirInfo);
                }
            }
        }
        else
        {
            success = false;
            errorMsg = "Please enter one or more filenames to search for.";
        }
    }
    else
    {
        success = false;
        errorMsg = "The directory\r\n" + m_pars.SearchDir + "\r\ndoes not exist.";
    }

    // Remember the thread has ended:
    m_thread = null;

    // Raise an event:
    if (ThreadEnded != null)
    {
        ThreadEnded(new ThreadEndedEventArgs(success, errorMsg));
    }
}

private static void SearchDirectory(DirectoryInfo dirInfo)
{
    if (!m_stop)
    {
        try
        {
            foreach (String fileName in m_pars.FileNames)
            {
                FileSystemInfo[] infos = dirInfo.GetFileSystemInfos(fileName);

                foreach (FileSystemInfo info in infos)
                {
                    if (m_stop)
                    {
                        break;
                    }

                    if (MatchesRestrictions(info))
                    {
                        // We have found a matching FileSystemInfo, 
                        // so let's raise an event:
                        if (FoundInfo != null)
                        {
                            FoundInfo(new FoundInfoEventArgs(info));
                        }
                    }
                }
            }

            if (m_pars.IncludeSubDirsChecked)
            {
                DirectoryInfo[] subDirInfos = dirInfo.GetDirectories();

                foreach (DirectoryInfo subDirInfo in subDirInfos)
                {
                    if (m_stop)
                    {
                        break;
                    }

                    // Recursion:
                    SearchDirectory(subDirInfo);
                }
            }
        }
        catch (Exception)
        {
        }
    }
}          

The stop is working fine I wanted to add also a pause button to pause and resume the thread. I added a button click event but how do I make the pause/resume actions and where?

This is a link for the complete code : https://pastebin.com/fYYnHBB6

Rufus L
  • 36,127
  • 5
  • 30
  • 43
Benzi Avrumi
  • 937
  • 1
  • 8
  • 24
  • One way to do this would be using SemaphoreSlim class. Check this out: https://stackoverflow.com/questions/19613444/a-pattern-to-pause-resume-an-async-task – Abhay May 29 '20 at 19:38

1 Answers1

0

You can use the ManualResetEvent object.

Here is the simplified version.

  1. In your class, create the object:

    public static ManualResetEvent _mrsevent = new ManualResetEvent(false);
    
  2. In your thread function, as part of the loops that search for files/directories:

    private static void SearchThread()
    {
       foreach (String fileName in m_pars.FileNames)
       {
          _mrsevent.WaitOne();
       }
    }
    
  3. From outside the thread you can call:

    a) _mrsevent.Set(); // resume the thread.

    b) _mrsevent.Reset(); // pause

JeremyRock
  • 396
  • 1
  • 8
  • The foreach you mentioned in your solution is in the SearchDirectory function not in the SearchThread function. In the SearchDirectory function there is two foreach loops but to add the _mrsevent.WaitOne(); only in the first foreach ? and I need to access the _mrsevent variable from form1 so maybe the _mrsevent should be public static ? – Benzi Avrumi May 29 '20 at 20:10
  • You could put the WaitOne() in any loop that might consume CPU. So yes - put it in both loops. Yes - make the variable public static - i updated the answer. – JeremyRock May 30 '20 at 13:21