并行运行一系列方法

时间:2016-06-16 10:24:27

标签: c#

我正在连接到REST API并调用许多端点来获取不同的对象。我为每个要下载的类型创建了<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xxx.testapp" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="9" /> <application android:label="@string/app_name"> <activity android:name="com.xxx.testapp.MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.xxx.testapp.UnityPlayerActivity" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:label="@string/app_name" android:launchMode="singleTask" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" /> </application> </manifest>

RestService<T>

对于每个RestService<Agent> agentService = new RestService<Agent>(auth, new AgentApi()); RestService<Ticket> ticketService = new RestService<Ticket>(auth, new TicketApi()); RestService<Company> companyService = new RestService<Company>(auth, new CompanyApi()); RestService<Contact> contactService = new RestService<Contact>(auth, new ContactApi()); 我然后调用RestService<T>来调用REST API并获得结果:

GetAll()

幕后RestResult<Agent> agentResults = agentService.GetAll(); RestResult<Company> companyResults = companyService.GetAll(); RestResult<Contact> contactResults = contactService.GetAll(); RestResult<Ticket> ticketResults = ticketService.GetAll(); 会产生许多GetAll()次请求。

所以我想的是以某种方式并行调用4 HttpWebRequest个调用,理论上我可以向REST API发出多个请求,而不是一个接一个地请求。

我的一个想法是:

GetAll()

但看起来变量从未被初始化。

有关如何处理此问题的任何建议?

2 个答案:

答案 0 :(得分:2)

编译器警告您变量未初始化,因为编译器不理解Parallel.Invoke()语义

所以编译器唯一知道的是你将一些lambda传递给那个函数。但它无法推断它们何时会被执行。编译器不知道Parallel.Invoke()仅在所有Action完成后才返回。它尤其不知道你的lambdas正在初始化变量。

因此,从编译器的角度来看,即使在Parallel.Invoke()之后,您还没有为变量赋值。

最简单的解决方案是使用默认值(null)手动初始化它们:

RestResult<Agent> agentResults = null;
RestResult<Company> companyResults = null;
RestResult<Contact> contactResults = null;
RestResult<Ticket> ticketResults = null;

Parallel.Invoke(
    () => agentResults = agentService.GetAll(Epoch),
    () => companyResults = companyService.GetAll(Epoch),
    () => contactResults = contactService.GetAll(Epoch),
    () => ticketResults = ticketService.GetAll(Epoch)
);

答案 1 :(得分:1)

看看这些问题,看起来就像你遇到的问题一样。 Parallel.Invoke似乎不等待异步操作。而是使用Task.WhenAll等待所有任务完成: Parallel.Invoke does not wait for async methods to complete

RestResult<Agent> agentResults;
RestResult<Company> companyResults;
RestResult<Contact> contactResults;
RestResult<Ticket> ticketResults;

var t1 = Task.Run(() => agentResults = agentService.GetAll(Epoch))
var t2 = Task.Run(() => companyResults = companyService.GetAll(Epoch));
var t3 = Task.Run(() => contactResults = contactService.GetAll(Epoch));
var t4 = Taks.Run(() => ticketResults = ticketService.GetAll(Epoch));

await Task.WhenAll(t1,t2,t3,t4);
//results should be filled here