开发者

C# Parallel Task methods that return value using Func<>

开发者 https://www.devze.com 2023-01-08 01:49 出处:网络
I want to use TASK for running parallel activities.This task returns a value.How do i use a FUNC inside the task for returning a value?.I get an error at Func.

I want to use TASK for running parallel activities.This task returns a value.How do i use a FUNC inside the task for returning a value?.I get an error at Func.

Task<Row> source = new Task<Row>(Func<Row> Check = () =>
{
    var sFields = (
        from c in XFactory.Slot("IdentifierA")
        where c["SlotID"] == "A100"
        select new
        {
            Field = c["Test"]
        });
});

I change my code to

Task source = Task.Factory.StartNew(() =>
            {
                return 
   开发者_如何学运维             (from c in XFactory.Slot("IdentifierA")
                  where c["SlotID"] == "A100"
                   select new
            {
                Field = c["Test"]
            });
            }
            );


Your basic pattern would look like:

 Task<Row> source = Task.Factory.StartNew<Row>(() => ...; return someRow;  );
 Row row = source.Result; // sync and exception handling
0

精彩评论

暂无评论...
验证码 换一张
取 消