简体   繁体   English

如何在c#中的另一个线程内实现线程

[英]How Implement Thread Within Another Thread in c#

Scenario: There is One Requests And Many Suppliers,So for getting Results From all Suppliers I use, 场景:有一个请求并且有很多供应商,所以为了从我使用的所有供应商那里获得结果,

var supplierResults = suppliers.AsParallel()
                           .WithDegreeOfParallelism(suppliers.Count())
                           .Select(supplier => supplier.GetResponse(request, SuppliersRequestTimespan))
                           .ToArray(); 

                    Task.WaitAll(supplierResults);

Which is Working Fine. 哪个工作正常。

In Another Scenario, I have Three Different Requests and Want to send it to all Suppliers Simultaneously(threads).I Just Tried Like Below: 在另一个场景中,我有三个不同的请求,想同时发送给所有供应商(线程)。我尝试如下:

int TaskCount = 3;
var tasks = new Task[TaskCount];

tasks[0] = Task.Factory.StartNew(() => (suppliers.AsParallel()
                                            .WithDegreeOfParallelism(suppliers.Count())
                                            .Select(supplier => supplier.GetResponse(request1, SuppliersRequestTimespan))
                                            .ToArray()));
tasks[1] = Task.Factory.StartNew(() => (suppliers.AsParallel()
                                                .WithDegreeOfParallelism(suppliers.Count())
                                                .Select(supplier => supplier.GetResponse(request2, SuppliersRequestTimespan))
                                                .ToArray()));
 tasks[2] = Task.Factory.StartNew(() => (suppliers.AsParallel()
                                                .WithDegreeOfParallelism(suppliers.Count())
                                                .Select(supplier => supplier.GetResponse(request3, SuppliersRequestTimespan))
                                                .ToArray()));

 Task.WaitAll(tasks);

But,Unfortunately It Not 'Wait for' the Response. 但是,不幸的是,它不是“等待”响应。

GetResponse() Method is From an Interface as showing Below: GetResponse()方法来自接口,如下所示:

 public interface ISupplier
    {
        Task<JObject> GetResponse(JObject request, TimeSpan timeout);

        Task<JObject> GetResponseAsync(JObject request, TimeSpan timeout);
        string GetSupplierBrand();
        string GetSupplierCulture();
    }

Hope You guys Help me to complete this.. 希望你们帮我完成这个任务。

Try using Microsoft's Reactive Framework for this. 尝试为此使用Microsoft的Reactive Framework。

Here's the main code: 这是主要代码:

var requests = new[] { request1, request2, request3 };

var query =
    from supplier in suppliers.ToObservable()
    from request in requests.ToObservable()
    from response in Observable.FromAsync(() =>
        supplier.GetResponseAsync(request, SuppliersRequestTimespan))
    select new { supplier, request, response };

Now, if you want to get all of the results as an array just call this: 现在,如果您想将所有结果作为数组获取,只需调用以下命令:

var results = query.ToArray().Wait();

But you can always use the Reactive Framework to get each result as soon as they come in. Just do this: 但是,您始终可以使用Reactive Framework尽快获得每个结果。只需执行以下操作:

var subscription =
    query
        .Subscribe(result =>
        {
            // Do something with each result
        });

All of this is multi-threaded and uses the maximum number of available threads. 所有这些都是多线程的,并使用最大数量的可用线程。

It's almost too simple. 这太简单了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM