3

まず、私は C# 初心者です。私のバックグラウンドは主にデータベースでした。私は、SQL Server DB からデータを取得するためにさまざまなストアド プロシージャ (約 20 程度) を呼び出す C# サーバーを頻繁に呼び出すプロジェクトに取り組んでいます。現在、C# サーバーは同期呼び出しを行うようにセットアップされています。SP 呼び出しは迅速かつ小規模ですが、大規模なユーザー プールと同時要求を処理するスレッド プールを実装したいと考えています。

私の質問:

  1. スレッドプールを実装するにはどうすればよいですか? ほとんどの場合、スレッド プールは 500 前後で開始されますが、アプリケーションの使用状況に応じて増加する可能性があります。

  2. SP 呼び出しをスレッド プールに追加する方法を教えてください。現在、私の SP 呼び出しは次のようになっています。

    int SPCall(string param1, string param2)
    {
      string MyConnString = "...";
      SqlConnection MyConn = new SqlConnection(MyConnString);
      MyConn.Open();
      SqlCommand SPCommand = new SqlCommand("wh_SP");
      SPCommand.Connection = MyConn;
      SPCommand.Parameters.Add(...) = param1;  
      SPCommand.Parameters.Add(...) = param2;  
    
      SPCommand.CommandType = System.Data.CommandType.StoredProcedure;
      SPCommand.ExecuteNonQuery();
      int outPut = (int)SPCommand.Parameters["@OUTPUT"].Value;
      return outPut;
     } 
    
4

1 に答える 1

0

コメントで述べたように、独自のものを実装する代わりに、.NET ThreadPool を使用する必要があります。さらに良いのは、新しい.NET Parallel ライブラリを使用して、これらをそれぞれ 1 つのタスクにまとめることです。比較的少ないコードで、同時実行の処理方法をより適切に制御できます。

public void PerformWork()
{
    // setup your inputs
    IEnumerable<string> inputs = CreateYourInputList();

    //  Method signature: Parallel.ForEach(IEnumerable<TSource> source, Action<TSource> body)
    Parallel.ForEach(inputs, input =>
        {
            // call your code that issues the stored procedure here
            this.SPCall(input);
        } //close lambda expression
    ); //close method invocation 

    // Keep the console window open in debug mode.
    Console.WriteLine("Processing complete. Press any key to exit.");
    Console.ReadKey();
}
于 2013-04-11T17:45:29.043 に答える