初期状況
.NET Framework 4.0、C#、Winform アプリケーションを開発しています。アプリケーションは、GridView に WebServiceOperations を一覧表示 (およびテスト) します (現在 60 DataRows => WebServiceOperations)。
目的
ボタンを1回クリックするだけで、このすべての操作をテスト/呼び出す必要があります。すべての操作で、クラスの新しいインスタンスが作成されます。このクラス内で、WebServiceOperation async を呼び出し、結果を待ちます。その後、結果が検証されます。デリゲートとイベントを使用すると、コード全体がスムーズに機能します。
次に、課題/質問について説明します。そのボタンをクリックすると、for ループ (int i = 0; i < gridViewWsOperations.RowCount; i++) => を使用します。つまり、現在、60 の操作を実行しています。同時に ' => サーバーが同時に 60 個のリクエストを処理する過負荷になり、タイムアウトが発生します。したがって、同時に 10 と言うように、同時リクエストの数をどうにかして調整する必要があります。for ループ (リクエストをエンキューする必要がある場所) が、リクエストをデキューするメソッド (process_result イベント) と同じスレッドにないことを考慮してください。このタイプのコレクションはスレッドセーフのように見えるため、ConcurrentQueue を使用して試しました。
リンク
サンプルコードは本当に役に立ちます!
--- これは私のソリューション/サンプル コードです ---
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Threading;
namespace ConcurrentQueueSample
{
class Program
{
static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(3);
static void Main(string[] args)
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Interval = 1234;
timer.Enabled = true;
timer.Start();
for (int i = 0; i < 10; i++) new Thread(go).Start(i);
}
static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
semaphoreSlim.Release();
}
static void go(object i)
{
Console.WriteLine("id: {0}", i);
semaphoreSlim.Wait();
Console.WriteLine("id: {0} is in", i);
Thread.Sleep(1250);
Console.WriteLine("id: {0} left!", i);
}
}
}