C# 定时器

using FreeSun.FS_SMISCloud.Server.CloudApi.DAL;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
//using Newtonsoft.Json;
using System.Data;

namespace 沉降
{
    class Program
    {
       // 定时
        private static void Wait(int Seconds)
        {

            for (int i = 0; i < Seconds; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    System.Threading.Thread.Sleep(100);
                }
            }
        }
        static void Main(string[] args)
        {
            bool isFirstLoad = true;

            while (true)
            {
                try
                {
                    string sql = string.Format("SELECT B.Granularity, SENSOR_ID,SAFETY_FACTOR_TYPE_ID ,A.MODULE_NO FROM    T_DIM_SENSOR  AS A LEFT JOIN T_DIM_DTU AS B  ON B.ID = A.DTU_ID WHERE B.Description =‘Http‘ order by Granularity;");

                    DataTable senserlist = SqlHelper.ExecuteDataSetText(sql, null).Tables[0];

                    if (!isFirstLoad)
                    {
                        int timeCount = int.Parse(senserlist.Rows[0]["Granularity"].ToString());
                        //表里取的,单位换算成秒
                       // Wait(timeCount * 60);                                                 
                        
                        Wait(10);
                    }
                    isFirstLoad = false;

                    int SENSOR_ID = 0, SAFETY_FACTOR_TYPE_ID = 0;
                    string MODULE_NO = "";
                   
                    string postDataStr = "沉降_压力modbus_br";
                  
                    for (int i = 0; i < senserlist.Rows.Count; i++)
                    {
                        SENSOR_ID = int.Parse(senserlist.Rows[i]["SENSOR_ID"].ToString());
                        SAFETY_FACTOR_TYPE_ID = int.Parse(senserlist.Rows[i]["SAFETY_FACTOR_TYPE_ID"].ToString());
                        MODULE_NO = senserlist.Rows[i]["MODULE_NO"].ToString();

                        string Url = "http://222.128.62.155:9666/pc.php/index/index/chaxun?table=" + postDataStr + MODULE_NO;


                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                        request.Method = "POST";
                        request.ContentType = "application/form-data";
                        // request.CookieContainer = cookie;

                        Stream myRequestStream = request.GetRequestStream();
                        StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
                        // myStreamWriter.Write(postDataStr);
                        myStreamWriter.Close();

                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                        //   response.Cookies = cookie.GetCookies(response.ResponseUri);
                        Stream myResponseStream = response.GetResponseStream();
                        StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                        string retString = myStreamReader.ReadToEnd();

                         JObject json1 = new JObject();
                        try
                        {
                            List<Info> jobInfoList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Info>>(retString);
                            if (jobInfoList[0].status == "0")
                            {
                                Console.WriteLine("请求方式错误");
                                break;

                            }
                            if (jobInfoList[0].status == "2")
                            {
                                Console.WriteLine("表名错误");
                                break;

                            }
                            if (jobInfoList[0].status == "3")
                            {
                                Console.WriteLine("开始时间错误");
                                break;

                            }
                            if (jobInfoList[0].status == "4")
                            {
                                Console.WriteLine("结束时间错误");
                                break;

                            }

                        }
                        catch (Exception ex)
                        {
                            json1 = (JObject)JsonConvert.DeserializeObject(retString);        
                           
                        }

                       

                                
                        var B = json1["c_time"].ToString();


                        var c_cz = json1["c_dqz"].ToString();

                        sql = string.Format("SELECT COUNT(*) from T_THEMES_DEFORMATION_SURFACE_DISPLACEMENT WHERE ACQUISITION_DATETIME =‘{0}‘", B);
                        DataTable datcout = SqlHelper.ExecuteDataSetText(sql, null).Tables[0];
                        if (datcout.Rows.Count > 1)
                        {
                            Console.WriteLine("时间" + B +"编号"+ MODULE_NO +""+c_cz+ "已存在该数据");
                        }
                        else
                        {
                            sql = string.Format("insert into T_THEMES_DEFORMATION_SURFACE_DISPLACEMENT(SENSOR_ID,SAFETY_FACTOR_TYPE_ID,SURFACE_DISPLACEMENT_X_VALUE,ACQUISITION_DATETIME) Values ({0},{1},{2},‘{3}‘) ", SENSOR_ID, SAFETY_FACTOR_TYPE_ID, c_cz, B);
                            SqlHelper.ExecteNonQueryText(sql, null);
                            Console.WriteLine("时间" + B + "编号" + MODULE_NO + "" + c_cz + "数据保存成功");
                        }

                        myStreamReader.Close();
                        myResponseStream.Close();
                    }


                }
                catch (Exception ex)
                {

                    throw;
                }
            }

            //return ; 

        }
    }

    public class Info
    {
        public string status { get; set; }
        public string mess { get; set; }
    }
}

 

C# 定时器

上一篇:Silverlight的Socket通信


下一篇:windows10去除ctrl+space空格键的占用