Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a problem with my tasks. When I try to recive returned variable from my task I can't use a .Result property to get it. Here is my code:

var nextElement = dir.GetValue(i++).ToString();
Task buffering = Task<byte[]>.Run(() => imageHashing(nextElement));
bitmapBuffer = buffering.Result;

and imageHasing function is declared like this:public bool[] imageHashing(string path)

I get an error saing:

Severity Code Description Project File Line Suppression State Error CS1061 'Task' does not contain a definition for 'Result' and no extension method 'Result' accepting a first argument of type 'Task' could be found (are you missing a using directive or an assembly reference?)

Example from this microsoft website works, and I can't understand why.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
179 views
Welcome To Ask or Share your Answers For Others

1 Answer

As others have noted, the compiler error is in your variable declaration (Task does not have a Result property):

var nextElement = dir.GetValue(i++).ToString();
var buffering = Task.Run(() => imageHashing(nextElement));
bitmapBuffer = buffering.Result;

However, this code is also problematic. In particular, it makes no sense to kick work off to a background thread if you're just going to block the current thread until it completes. You may as well just call the method directly:

var nextElement = dir.GetValue(i++).ToString();
bitmapBuffer = imageHashing(nextElement);

Or, if you are on a UI thread and do not want to block the UI, then use await instead of Result:

var nextElement = dir.GetValue(i++).ToString();
bitmapBuffer = await Task.Run(() => imageHashing(nextElement));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...