Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# execute method on other thread

i have a method that read some files and get hashes SHA1Managed and then compare it with other hashes from a list, how can i do this method on other thread?

public bool CheckFile(string file, string filehash) 
    {

            if (File.Exists(file))
            {

                using (FileStream stream = File.OpenRead(file))
                {
                    SHA1Managed sha = new SHA1Managed();
                    byte[] checksum = sha.ComputeHash(stream);
                    string sendCheckSum = BitConverter.ToString(checksum)
                        .Replace("-", string.Empty);

                   return sendCheckSum.ToLower() == filehash;
                }
            }
            else return false;

    }
like image 785
MozzieMD Avatar asked Sep 27 '22 21:09

MozzieMD


1 Answers

If you just want to run it in a background thread you'd actually need to move the task creation up one level since your function returns a result. Depending on how the calling code works something like this might work for you.

var backgroundTask = Task.Factory.StartNew(() =>
{
    var result = CheckFile("file", "filehash");
    //do something with the result
});
like image 77
Nicholas J. Markkula Avatar answered Nov 08 '22 22:11

Nicholas J. Markkula