using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Threading;
|
using System.IO;
|
using System.Data;
|
using System.Data.SqlClient;
|
using Microsoft.Practices.EnterpriseLibrary.Data;
|
using System.Collections;
|
using System.Reflection;
|
using System.ComponentModel;
|
|
namespace ThreadTestSMGW
|
{
|
class Program
|
{
|
//private static int poolFlag = 0;//声明标记
|
//private const int amountThread = 10000;//线程总量
|
//private const int maxThread = 10000;//可执行线程最大数量
|
//private static Mutex muxConsole = new Mutex();
|
|
//初始化短信发送参数
|
private static string account = System.Configuration.ConfigurationManager.AppSettings["account"];
|
private static string password = System.Configuration.ConfigurationManager.AppSettings["password"];
|
private static string mobile = System.Configuration.ConfigurationManager.AppSettings["mobiles"];
|
private static string extno = System.Configuration.ConfigurationManager.AppSettings["extno"];
|
private static string content = System.Configuration.ConfigurationManager.AppSettings["content"];
|
private static string ThreadNum = System.Configuration.ConfigurationManager.AppSettings["ThreadNum"];//线程总数
|
private static string CycNum = System.Configuration.ConfigurationManager.AppSettings["CycNum"]; //循环次数
|
|
private static int startTime = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["startTime"]);
|
private static int endTime = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["endTime"]);
|
public static string conn = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
|
|
private static int CountNum = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["Count"]);
|
|
|
public static bool listFlag = true; //插入成功标识
|
static void Main(string[] args)
|
{
|
//Thread.Sleep(10000);
|
Console.WriteLine("开始执行...");
|
|
Thread threads = new Thread(new ThreadStart(DealSMS));
|
threads.Start();
|
|
//Thread[] threads = new Thread[Int32.Parse(ThreadNum)];
|
//for (int i = 0; i < Int32.Parse(ThreadNum); i++)
|
//{
|
// threads[i] = new Thread(DealSMS);
|
// threads[i].IsBackground = true;
|
// threads[i].Name = "线程" + i;
|
// threads[i].Start();
|
|
//}
|
|
//Thread listThread = new Thread(listTime);//列表处理
|
//listThread.Name = "列表线程";
|
//listThread.Start();
|
|
//Thread ReportThread = new Thread(GetReport);
|
//ReportThread.Start();
|
|
|
|
//bool flag = true;
|
|
|
//while (true)
|
//{
|
// for (int i = 0; i < Int32.Parse(ThreadNum); i++)
|
// {
|
// if (threads[i].IsAlive == false)
|
// { flag = false; }
|
// else { flag = true; }
|
// }
|
// if (flag == false)
|
// {
|
// if (listFlag==false)
|
// {
|
// //Console.WriteLine("统计插入完成");
|
// listThread.Abort();
|
// break;
|
// }
|
|
// }
|
//}
|
//Console.ReadKey();
|
|
}
|
|
static void GetReport()
|
{
|
//while (true)
|
//{
|
// SMSHandler sms = new SMSHandler();
|
// sms.Report(account, password);
|
// Thread.Sleep(2000);
|
//}
|
}
|
|
|
//插入列表
|
public static List<SMSModel> smsList = new List<SMSModel>();
|
private static object olock = new object();
|
public static void addList(string smsid,string starttime,string endtime,string content)
|
{
|
lock (olock)
|
{
|
smsList.Add(new SMSModel() { smsid = smsid, starttime = starttime, endtime = endtime,content=content});
|
}
|
}
|
public static int Count = 0;
|
//移除列表
|
static void listTime()
|
{
|
Random r = new Random();
|
int[] a =new int[60];
|
int iii = 1000;
|
for (int i = 0; i < 60; i++)
|
{
|
a[i] = iii;
|
iii = 1000 + iii;
|
}
|
|
int temp = a[r.Next(59)];
|
|
while (true)
|
{
|
try
|
{
|
if (smsList.Count == CountNum)
|
{
|
Console.WriteLine("统计线程阻塞:" + temp);
|
Thread.Sleep(temp);
|
|
Console.WriteLine("统计开始...");
|
DataSql ds = new DataSql();
|
ListToDt ltd = new ListToDt();
|
ds.SqlBulkCopyByDatatable(conn, "tb_cs", ltd.ListToDataTable<SMSModel>(smsList));
|
Console.WriteLine("统计结束...");
|
listFlag = false;
|
break;
|
}
|
}
|
catch (Exception ex)
|
{ Console.WriteLine(ex.Message); }
|
|
/*
|
for (int i = 0; i < smsList.Count; i++)
|
{
|
DataSql ds = new DataSql();
|
try
|
{
|
int ii = ds.InsertSql(smsList[i].starttime, smsList[i].endtime);//插入数据库
|
if (ii > 0)
|
{
|
++Count; smsList.RemoveAt(i);
|
}
|
else
|
{
|
Console.WriteLine("数据库插入异常..");
|
}
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine(ex.Message);
|
}
|
}
|
*/
|
}
|
|
}
|
|
|
|
|
//线程处理
|
static void DealSMS()
|
{
|
Thread.CurrentThread.Name = "发送线程";
|
var dt = DateTime.Now;
|
if(true)
|
{
|
int i = 0;
|
while (i < Int32.Parse(CycNum))
|
//while(true)
|
{
|
i++;
|
SMSHandler sms = new SMSHandler();
|
//TornadoSMSHandler sms = new TornadoSMSHandler();
|
string addTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
|
string result = sms.SendSMS(account, password, extno, mobile, content);//迈远网关
|
Console.WriteLine(content);
|
//StrToHex sth = new StrToHex();
|
//string content2 = sth.ToHexString(System.Text.Encoding.Default.GetBytes(content));
|
|
//string result = sms.SendSMS(account,password,mobile,content2);
|
|
//string[] sArray = result.Split(',');
|
|
|
//addList(sArray[0], addTime, sArray[1], content2);
|
|
//点对点短信提交
|
|
//string mobileConentList = "13611452175#【掌骏】您的短信码是322734,有效期为30分钟。若非本人操作,请及时修改密码。\n692676113#【掌骏】您的短信码是322734,有效期为30分钟。若非本人操作,请及时修改密码。";
|
//string result = sms.SendSMS(account,password,extno,mobileConentList);
|
|
//Console.WriteLine(mobileConentList);
|
string str = string.Format("[{0}]--提交时间:{1}--提交结果:{2}", Thread.CurrentThread.Name, addTime, result);
|
//WriteLog(str);
|
Console.WriteLine(str);
|
|
Console.ReadKey();
|
Thread.Sleep(10);
|
|
}
|
|
Console.WriteLine("["+Thread.CurrentThread.Name+"]--程执行完毕..."+i);
|
Thread.CurrentThread.Abort();
|
}
|
}
|
|
|
|
static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim();
|
//写日志
|
public static void WriteLog(string str)
|
{
|
try
|
{
|
LogWriteLock.EnterWriteLock();
|
string sFilePath = System.IO.Directory.GetCurrentDirectory();
|
string sFileName = DateTime.Now.Date.ToString("yyyy-MM-dd")+"-sms.log";
|
sFileName = sFilePath + "\\" + sFileName;
|
if (!Directory.Exists(sFilePath))
|
{
|
Directory.CreateDirectory(sFilePath);
|
}
|
FileStream fs;
|
StreamWriter sw;
|
if (File.Exists(sFileName))
|
{
|
fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
|
}
|
else
|
{
|
fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
|
}
|
sw = new StreamWriter(fs);
|
sw.WriteLine(str);
|
sw.Close();
|
fs.Close();
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine(ex.Message);
|
}
|
finally
|
{
|
LogWriteLock.ExitWriteLock();
|
}
|
}
|
|
|
|
|
|
|
|
|
|
|
#region 注释掉的
|
/*
|
public static void Run()
|
{
|
muxConsole.WaitOne(); //阻塞队列
|
Interlocked.Increment(ref poolFlag); //标记+1
|
if (poolFlag < maxThread) //判断是否达到线程上限
|
{
|
muxConsole.ReleaseMutex(); //释放队列锁,加入执行线程
|
}
|
//Console.WriteLine("{0} 正在运行......\n", Thread.CurrentThread.Name);
|
|
SMSHandler sms = new SMSHandler();
|
string errmsg = "";
|
bool flag = sms.SendSMS(account,password,extno,mobile,content,out errmsg);
|
|
|
//Thread.Sleep(3000); //模拟执行,暂停3秒钟
|
//Console.WriteLine("{0} 已经中止....{1}..\n", Thread.CurrentThread.Name,errmsg);
|
|
Interlocked.Decrement(ref poolFlag); //标记-1
|
try
|
{
|
muxConsole.ReleaseMutex(); //释放阻塞
|
}
|
catch { }
|
}
|
public static void Run2()
|
{
|
SMSHandler sms = new SMSHandler();
|
content = "【测试】线程,开始执行测试,时间:" + DateTime.Now.ToString();
|
string errmsg = "";
|
sms.SendSMS(account, password, extno, mobile, content, out errmsg);
|
Console.WriteLine("{0} 已经中止....{1}..\n", Thread.CurrentThread.Name, errmsg);
|
}
|
*/
|
#endregion
|
}
|
}
|