簡體   English   中英

需要在C#控制台應用程序中實現線程和信號量

[英]Need to implement Threading and semaphore in C# console application

目標是觸發url(來自報告服務器的報告)。

我有一個sql請求返回行。 假設它返回1000行,我需要一次批量或以100個塊觸發所有這些URL,以便服務器性能不會受到影響。

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", "India@");
                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; }
    }
}

將給您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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM