单线程与多线程的简单示例(以Windows服务发短信为示例)

单线程示例:

public delegate void SM();

SM sm = new SM(() =>
    {
                    while (true)
                    {

          //读取发短信列表

          if(有数据)

             //发短信之后把短信标识改掉,防止重复发短信

          else

          {

            //使用下一个时间间隔唤醒线程
                         System.Threading.Thread.Sleep(1000); //每秒读取一次

          }

        }

  }

     sm.BeginInvoke(null, null);

 }

多线程示例:

private Thread thread = null;

 protected override void OnStart(string[] args)
{
            try
            {
                thread = new Thread(new ThreadStart(StartSendSmsMessages));
                thread.IsBackground = true;
                if ((thread != null) && (!thread.IsAlive))
                {
                    Common.WriteLog("当前线程已启动!");
                    thread.Start();
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("开启服务异常为:" + ex.Message);
            }           
}

 private void StartSendSmsMessages()
        {
            while (true)
            {
                try
                {
                    DataTable dt = DataToDalorBll.ReadWaitSMSData(SQLConnectionStringResult, string.Format(ReadDataInfo, ReadCount));   //读取待发送短信列表
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        //假设有100个请求线程
                        int ThreadCount = Convert.ToInt32(SendCount);  //SendCount表示线程数量
                        int BusCount = (int)Math.Ceiling((double)dt.Rows.Count / ThreadCount);//根据行数,线程数设定每个线程要处理的数据量
                        if (ConnSUM != 0) { ConnSUM = 0; };

                        System.Diagnostics.Stopwatch sch = new System.Diagnostics.Stopwatch();
                        sch.Start();
                        using (var countdown = new MutipleThreadResetEvent(ThreadCount))
                        {
                            for (int k = 0; k < BusCount; k++)
                            {
                                for (int i = 0; i < ThreadCount; i++)
                                {
                                    DataRow dr = dt.Rows[i + k * ThreadCount];
                                    Common.WriteLog("当前行数据:" + (i + k * ThreadCount).ToString());
                                    //开启N个线程,传递MutipleThreadResetEvent对象给子线程
                                    ThreadPool.QueueUserWorkItem(new WaitCallback(SendSmsInfo), dr);
                                    countdown.SetOne();
                                }
                                Thread.Sleep(Convert.ToInt32(SendSleepTime)); 

                            }
                            //等待所有线程执行完毕
                            countdown.WaitAll();
                            //释放空间
                            countdown.Dispose();
                        }
                        sch.Stop();
                        Common.WriteLog("当前运行时间:" + sch.Elapsed.ToString());
                    }
                    else
                    {
                        Thread.Sleep(Convert.ToInt32(TimeReadData));
                    }
                }
                catch (Exception ex)
                {
                    Common.WriteLog("发送短信异常:" + ex.Message);
                    Thread.Sleep(Convert.ToInt32(ConnectionTime));
                    Common.WriteLog("取数出错,即将重新去取数据!");
                }
            }
        }

//发短信方法

private void SendSmsInfo(object obj)
  {

  //删除待发送列表中的(DataRow)obj中的一条数据

  //开始调用发短信方法

  //发短信成功后将这条数据和返回值插入已发送列表
  }

protected override void OnStop()
{
            if ((thread != null) && (thread.IsAlive))
            {
                Common.WriteLog("当前线程已停止!");
                thread.Abort();
            }
}

短信的发送主要是以Windows服务为主,调试方法主要以日志的方式进行记录

安装或者卸载服务的方法为:(v4.0.30319为当成程序的编译版本 [(/u)为卸载] 当前程序的bin\Debug\XX.exe)

C:\windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe (/u)  E:\DoWeiSendEmailWindowsService\bin\Debug\DoWeiSendEmailWindowsService.exe

posted @ 2014-01-15 09:59  silence逢场作戏  阅读(1033)  评论(0编辑  收藏  举报