等待一分钟,然后在c#

时间:2018-12-01 10:36:14

标签: c# async-await azure-functions delayed-execution

我想在每个api调用之间应用延迟/等待。我正在使用队列触发器。第一个函数将创建API并将api存储在队列中,下一个函数将调用api并获取响应。我希望我的函数在第一次调用api之后等待一分钟,然后再调用下一个api,依此类推。 我已经申请

System.Threading.Thread.Sleep(TimeSpan.FromSeconds(60));

但这将等待一个分钟以调用第一个api,一旦延迟完成,它将立即调用队列中的所有api。

[2018年1月12日 10:11 :05]延迟了一次最小化的https://weblink.com/api/v2/topics?patentID=10 [01/12/2018 10:11 :06]延迟了最低的apicall https://weblink.com/api/v2/topics?patentID=11 [01/12/2018 10:11 :06]延迟了最低的apicall https://weblink.com/api/v2/topics?patentID=12 [01/12/2018 10:11 :06]延迟了最低的apicall https://weblink.com/api/v2/topics?patentID=13

我期待

[2018年1月12日 10:11 :05]延迟了一次最小化的https://weblink.com/api/v2/topics?patentID=10 [01/12/2018 10:12 :06]延迟了最低的apicall https://weblink.com/api/v2/topics?patentID=11 [01/12/2018 10:13 :06]延迟了最低的apicall https://weblink.com/api/v2/topics?patentID=12 [01/12/2018 10:14 :06]延迟了最低限度的https://weblink.com/api/v2/topics?patentID=13

示例代码: 创建api调用的代码已放入队列。

public static void Run([TimerTrigger("0 40 15 * * *")]TimerInfo myTimer, [Queue("topicque")] ICollector<string> outputQueueItem, TraceWriter log)
    {
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(" https://weblink.com/api/v2/parent");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "GET";
        httpWebRequest.Headers["Authorization"] =""
        try
        {
            using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse())
            {
                Stream responseStream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream);
                str4 = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
            }
            foreach (Cat.cat cat in JsonConvert.DeserializeObject<List<Cat.cat>>(str4))
            {
            long id=cat.id
            ICollector<string> collector = outputQueueItem;
            string str5 = " https://weblink.com/api/v2/topics?patentID="+id
            collector.Add(str5);
            }
            }

从队列触发并调用api的代码

public static void Run([QueueTrigger("topicque")] string myQueueItem, [Queue("topicque")] ICollector<string> outputQueueItem2, TraceWriter log)
    {

    System.Threading.Thread.Sleep(TimeSpan.FromSeconds(60));
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(myQueueItem);
    httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "GET";
         try
        {
            using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse())
            {

                Stream responseStream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream);
                str3 = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
                }
                }
                }

有什么办法可以实现这一目标?

谢谢

0 个答案:

没有答案