简体   繁体   中英

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:

 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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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