5

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;

    }
MozzieMD
  • 355
  • 2
  • 12
  • Where is the other thread? – Krease May 20 '15 at 03:49
  • Perhaps you can use a delegate as per this post: http://stackoverflow.com/questions/811224/how-to-create-a-thread – rivanov May 20 '15 at 03:53
  • I don't think you understood me guys, i need a way to run this method async! Right now , it's blocking my UI! – MozzieMD May 20 '15 at 04:00
  • There are probably a dozen different ways to run some code on a thread other than the current one. And there are probably thousands of related posts on Stack Overflow on the topic already. Please do a little research, figure out what technique you want to use, and if you have problems with that technique, post a _specific_ question about that. – Peter Duniho May 20 '15 at 04:00
  • @PeterDuniho i tried many of them, and couldn't make it work, so that's why i'm here! – MozzieMD May 20 '15 at 04:02
  • 2
    If you tried _any_ of them, you need to _show your work_. Don't make people start from scratch, trying to guess what's already been tried and didn't work. Include [a good, _minimal_, _complete_ code example](http://stackoverflow.com/help/mcve) that shows clearly what you've tried, and explain in precise detail what that code does and how that's different from what you wanted it to do. – Peter Duniho May 20 '15 at 04:07

2 Answers2

4

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
});
Nicholas J. Markkula
  • 1,542
  • 4
  • 18
  • 30
0

Try by this codes:

public async Task<bool> CheckFile(string file, string filehash) 
{
     await Task.Run<bool>(()=> {
        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;
     });
}
Behzad
  • 3,502
  • 4
  • 36
  • 63
  • 2
    Just you must to know this codes run at .NET Framework 4.5 or later versions, And if you want to use .NET v4.0 then you should use Task without async only. [References](https://msdn.microsoft.com/en-us/library/hh191443.aspx) – Behzad May 20 '15 at 04:08
  • You don't need to specify the `Run()` method's type parameter. And this answer isn't very useful unless you demonstrate how the OP should call it from their code. Oh, wait...you don't know what their calling code looks like? Ooops. – Peter Duniho May 20 '15 at 04:08