wzp
2021-07-30 afda40ef498cca59e58b2b6869deae90f3c13de5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
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
    }
}