2013-02-26 39 views
0

目标是触发一个url(来自报告服务器的报告)。需要在C#控制台应用程序中实现线程和信号量

我有一个sql-request返回行。假设它返回1000行,我需要一次触发所有这些URL,批量或100个块,以便服务器性能不会受到影响。

using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Net; 
namespace ConsoleApplication1 
{ 
    class Program 
    { 

     static void Main(string[] args) 
     { 

      Console.WriteLine("hello"); 
      string connectionString = "Server=Mayank-Pc;Database=reportserver;User=sa;Password=mayank;Trusted_Connection=False;"; 
      SqlConnection conn = new SqlConnection(connectionString); 
      conn.Open(); 
      Console.WriteLine("done"); 

      string strSQLQuery = string.Empty; 
      strSQLQuery = @"SELECT top 3 'mayank-pc' AS ServerName,C.Path AS ReportPath,'salesorderid=43659' Parameter,0 MaxTimeDataRetrievalSeconds,0 MinTimeDataRetrievalSeconds,99 TotalCount FROM Catalog C where path like '/Reports/Sales%'"; 
      SqlCommand cmd = new SqlCommand(strSQLQuery, conn); 
      SqlDataAdapter adapt = new SqlDataAdapter(cmd); 
      System.Data.DataTable table = new System.Data.DataTable("allPrograms"); 
      adapt.Fill(table); 

      int dtCount; 
      dtCount = table.Rows.Count; 
      Console.WriteLine(dtCount); 
      foreach (DataRow dr in table.Rows) 
      { //http:// mayank-pc/ReportServer/Pages/ReportViewer.aspx?/Reports/Sales&rs:Command=Render&salesorderid=43659 
       string strPath = "http://" + dr["ServerName"].ToString() + "/ReportServer/Pages/ReportViewer.aspx?" + dr["ReportPath"].ToString() + "&rs:Command=Render&" + dr["Parameter"].ToString(); 
       Console.Write(strPath + "\n"); 
       //System.Diagnostics.Process.Start("iexplore", strPath); 
       WebRequest myRequest = WebRequest.Create(strPath); 
       myRequest.Credentials = new NetworkCredential("mayank", "[email protected]"); 
       myRequest.Method = "GET"; 
       myRequest.PreAuthenticate = true; 
       // Return the response. 

       try 
       { 
        WebResponse myResponse = myRequest.GetResponse(); 
        Console.Write("Success" + "\n"); 
       } 
       catch (WebException e) 
       { 
        Console.Write("Error:" + e.ToString() + "\n"); 
       }     
      } 
      Console.Read();   
     } 

     public SqlConnection objConn { get; set; } 
    } 
} 
+0

首先,你应该构造你的方法approprietly。创建一个工作方法来处理您的请求。但我相信你将无法同时运行100个请求... – 2013-02-26 07:55:32

回答

0

会给你100级的任务,然后等待所有完成。如果你想让它重新运行的话,把它放在某种时候。

 int threads = 100; 
     Task[] tasks = new Task[threads]; 

     for (int i = 0; i < threads; i++) 
     { 
      tasks[i] = Task.Factory.StartNew(() => 
       { 
        //Dostuffs 
       }); 
     } 
     Task.WaitAll(tasks); 
相关问题