【发布时间】:2021-04-12 04:22:56
【问题描述】:
我的 asp.net 控制台应用程序中有以下代码,用于在 20 个请求的同时调用名为 gettingCustomerInfo 的方法,如下所示:
class Program
{
static SemaphoreSlim throttler = new SemaphoreSlim(initialCount: 20);
private static async Task<ScanInfo> gettingCustomerInfo(string website,
string phone, long companyId)
{
await throttler.WaitAsync();
ScanInfo si = new ScanInfo() { companyId = companyId };
try
{
//code goes here...
}
finally
{
throttler.Release();
}
}
static async Task Main(string[] args)
{
bool hasmore = true;
string offset = string.Empty;
bool first = true;
try
{
while (hasmore || first)
{
Marketing ipfd = new Marketing();
first = false;
try
{
// call the PM API to get the account id
using (WebClient wc = new WebClient())
{
wc.Encoding = Encoding.UTF8;
string url = "https://api.hubapi.com/companies/v2/companies/" +
"paged?hapikey=******properties=website" +
"&properties=mse_scan&properties=phone" +
"&limit=" + 100 + "&offset=" + offset;
string tempurl = url.Trim();
var json = wc.DownloadString(tempurl);
ipfd = JsonConvert.DeserializeObject<Marketing>(json);
int rrr = ipfd.companies.Count();
offset = ipfd.offset;
hasmore = ipfd.hasmore;
}
}
catch (Exception e)
{
}
var tasks = ipfd.companies
.Select(c => Task.Run(() => gettingCustomerInfo
(
c.properties.website.value,
(
c.properties.phone != null &&
!String.IsNullOrEmpty(c.properties.phone.value) ?
c.properties.phone.value : null
),
c.companyId
)
));
var results = await Task.WhenAll(tasks);
}
}
}
}
现在我迷失了我的代码的逻辑和流程将如何运行?现在在 API 调用中我得到 100 个项目,然后在 gettingCustomerInfo 中我定义了一个 SemaphoreSlim = 20。但我不确定我的代码流程如何?谁能给点建议?
Edit-1
现在我在 gettingCustomerInfo 方法中添加了以下内容:-
private static async Task<ScanInfo> gettingCustomerInfo(string website,string phone, long compnayId)
{
Console.WriteLine("a");
await throttler.WaitAsync();
ScanInfo si = new ScanInfo() { companyId = compnayId };
Console.WriteLine("b");
我得到了这个模式:-
a
a
a
a
a
a
a
b
b
a
b
a
b
b
b
a
b
a
b
b
a
b
b
b
a
b
a
b
a
b
a
b
a
b
【问题讨论】:
-
为什么不运行调试器并单步执行代码以查看执行情况?
-
@LarryBud 不幸的是,可能有很多线程处于活动状态,调试可能是一个挑战,因为调试器不断跳到另一个活动线程
-
@LarryBud 我试图这样做,但我迷失了流程将如何运行
-
我认为您问题中的代码有一些与您的要求无关的噪音,可以删除。但是在斯蒂芬克利里的回答之后,这可能并不重要。我认为没有人能提供更好的答案。 :-)
标签: c# asp.net asynchronous parallel-processing