2014-11-02 2 views
0

양식 응용 프로그램을 작성하고 while 루프에서 while 루프를 반복하면서 결과를 찾을 때까지 누릅니다. 그러나이 루핑은 일부 서버를 요청하고 있습니다. 이 요청을 최대 5 건의 요청에 1 분 안에 집중시키고 싶습니다. 따라서 새로운 분이 시작될 때까지 잠 들어있는 논리가 필요합니다. 누군가 나를 도울 수 있습니까?win form 응용 프로그램에서 분당 5 건의 요청을 처리하는 방법

 public int RPMCounter { get; set; } 

     private async void SearchCheapestAuction() 
     { 
      bool foundItem = false; 

      textBoxLogging.Clear(); 
      textBoxLogging.Text += System.Environment.NewLine + "start"; 

      // 1 stay loooping till you found this item for the buynowprice 
      while (!foundItem) 
      { 
       // 2 check if this is request number 5 in one minute 
       if (RPMCounter <= 5) 
       { 
        // 3 increase counter 
        RPMCounter++; 

        // 4 set searchparameters 
        var searchParametersPlayers = new PlayerSearchParameters 
        { 
         MaxBid = (uint)Convert.ToInt16(textBoxMaxStartPrice.Text), 
         MinBid = (uint)Convert.ToInt16(textBoxMinStartPrice.Text), 
         MaxBuy = (uint)Convert.ToInt16(textBoxMaxBuyNow.Text), 
         MinBuy = (uint)Convert.ToInt16(textBoxMinBuyNow.Text) 
        }; 

        // 5 run search query 
        var searchResponse = await client.SearchAsync(searchParametersPlayers); 

        // 8 check if the search found any results 
        if (searchResponse.AuctionInfo.Count > 0) 
        { 

         // 9 buy this player for the buy now price 
         var auctionResponse = await client.PlaceBidAsync(searchResponse.AuctionInfo.First(), searchResponse.AuctionInfo.First().BuyNowPrice); 

         // 10 stop searching/buying, I found my item for the right price 
         return; 
        } 
       } 
       else 
       { 
        // 11 I access the 5 rpm, sleep till the next minutes begin and go search again? 
        return; 
       } 
      } 

      textBoxLogging.Text += System.Environment.NewLine + "finished"; 
     } 
} 

답변

1

내가 이런 식으로 처리 할 것 :

여기 내 코드입니다.
이 방법은 다음과 같은 효과가 있습니다. 임의로 짧은 간격으로 5 번 연속으로 서버를 요청한 다음 1 분 동안 기다렸다가 임의로 짧은 간격으로 5 번 연속으로 다시 호출합니다.
그게 당신이하려는 의도라면, 왜 그렇게 필요한지 설명 할 수 있습니까?
System.Timers.Timer 간격을 12 초로 설정하고 요청이 완료되었는지 확인하여 통화 수를 분당 5로 제한 할 수 있습니다.
해당 항목을 찾지 못했다면 새 항목을 만들 수 있고 그렇지 않은 경우 다음에 타이머가 경과 할 때까지 기다릴 수 있습니다.

그것은이 같은 것을 볼 수 있었다 :

private Timer _requestTimer; 
private readonly object _requestLock = new object(); 
private bool _requestSuccessful; 

private void StartRequestTimer() 
{ 
    _requestTimer = new Timer(12 * 1000) { AutoReset = true }; 
    _requestTimer.Elapsed += requestTimer_Elapsed; 
    _requestTimer.Start(); 
} 

void requestTimer_Elapsed(object sender, ElapsedEventArgs e) 
{ 
    lock (_requestLock) 
    { 
     if (_requestSuccessful) 
     { 
      _requestTimer.Stop(); 
     } 
     else 
     { 
      TryNewRequest(); 
     } 
    } 
} 

private void TryNewRequest() 
{ 
    lock (_requestLock) 
    { 
     //try a new asynchronous request here and set _requestSuccessful to true if successful 
    } 
} 

를 메인 함수에서 먼저 TryNewRequest() 부를 것이다 다음 StartRequestTimer()을 부를 것이다. 요청이 제대로 작동하려면 비동기 적이어야합니다.

+0

안녕하세요, Matt, 답변 해 주셔서 대단히 감사합니다. 네, 구현 목표 : 1 분당 5 개로 제한합니다. 12 초 간격으로 타이머로 제발 나를 위해 코드에 몇 가지 예가 있습니까? – Ola

+0

이 (가) 게시물을 편집하고 일부 코드를 추가했습니다. 잘하면 도움이됩니다. –

관련 문제