需要在C#控制台应用程序中实现Threading [英] Need to implement Threading in C# console application

查看:118
本文介绍了需要在C#控制台应用程序中实现Threading的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是触发一个网址(来自报告服务器的报告)。



我有一个返回行的SQL,假设它返回1000行,我需要触发所有这些URL,一次批量或100块,这样服务器性能不会受到阻碍。

以下是我的代码。请建议我如何实施。



谢谢

Mayank

Goal is to trigger a url ( reports from the report server).

I''ve a sql which return rows suppose it returns 1000 rows , i need to trigger all those urls, in batch or in chunk of 100 at one time so that server performance will not hamper.
below is my code. Please suggest how will i implement .

Thanks
Mayank

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.IO;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Threading;


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);

            /* Start  Implementing Threading*/    
            

          /* Start  Implementing Threading*/

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

推荐答案

所有你需要的你可以在这里找到:



http://msdn.microsoft.com/en -us / library / system.threading.semaphore.aspx [ ^ ]。



但是,请看我对这个问题的评论:你没有真正的理由使用信号量。你需要彻底思考它;例如,到目前为止,这个问题并不像通常的互斥一样,只能通过锁定语句获得。确保你清楚地理解它:

http://en.wikipedia.org/wiki/Mutual_exclusion [ ^ ],

< a href =http://en.wikipedia.org/wiki/Semaphore_%28programming%29> http://en.wikipedia.org/wiki/Semaphore_%28programming%29 [ ^ ]。



-SA
All you need you can find here:

http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx[^].

However, please see my comments to the question: you don''t really have a justification for using a semaphore. You need to think about it thoroughly; the problem is by far not that trivial as with usual mutual exclusion you can get with a lock statement, for example. Make sure you clearly understand it:
http://en.wikipedia.org/wiki/Mutual_exclusion[^],
http://en.wikipedia.org/wiki/Semaphore_%28programming%29[^].

—SA




你试过这个:



Hi,
Have you tried this:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Threading.Tasks;


namespace Domain
{
    class Program
    {        
        public SqlConnection objConn { get; set; }

        public 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);
 
            /* Start  Implementing Threading*/    
            
 
          /* Start  Implementing Threading*/
            List<string> urls = new List<string>();
            foreach (DataRow dr in table.Rows)
            {
                string strPath = "http://" + dr["ServerName"].ToString() + "/ReportServer/Pages/ReportViewer.aspx?" + dr["ReportPath"].ToString() + "&rs:Command=Render&" + dr["Parameter"].ToString();
                urls.Add(strPath);
                
                Console.Write(strPath + "\n");             
            }
            
            DownloadAsynchronous(urls);

            Console.Read();
        
        }

        private  void DownloadAsynchronous(List<string> urls)
        {
            Parallel.ForEach<string>(urls, url =>
            {
                Download(url);
            });
        }

        private void Download(object strPath)
        {
            WebRequest myRequest = WebRequest.Create(strPath.ToString());
            myRequest.Credentials = new NetworkCredential("mayank", "India@1985");
            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");
            }
        }

    }
}





你有很多在代码中不需要的引用,如果没有使用,请尝试不包含引用。



我希望这会有所帮助。



问候

Jegan



you have lot of unwanted references in your code,try not include references if it is not used.

I hope this helps.

Regards
Jegan


这篇关于需要在C#控制台应用程序中实现Threading的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆