工作体会(一)
(1)
 DateTime.Now.ToString("yyyyMMdd")
DateTime.Now.ToString("yyyyMMdd")
显示时间
 DateTime.Now.ToString("hhmmssff")
DateTime.Now.ToString("hhmmssff")
(2)
使用枚举
 enum IC_CardState{unuse=0,natural,lose,freeze,logout,BeforeDate}
enum IC_CardState{unuse=0,natural,lose,freeze,logout,BeforeDate}
 if ((IC_CardType)dr_Result["CardType"]!=IC_CardType.fixCard)
if ((IC_CardType)dr_Result["CardType"]!=IC_CardType.fixCard)
 {
                {
 if (System.DateTime.Now>=Convert.ToDateTime(dr_Result["EndDate"]))
                    if (System.DateTime.Now>=Convert.ToDateTime(dr_Result["EndDate"]))
 {
                    {
 dr_Result["AfterStatus"]=(int)IC_CardState.BeforeDate; //过期卡
                        dr_Result["AfterStatus"]=(int)IC_CardState.BeforeDate; //过期卡
 }
                    }
 }
以上枚举使用的的一个用例
                }
以上枚举使用的的一个用例
(3)
字符串换行
 MessageBox.Show("aaaa" + Environment.NewLine + "bbbb");
(4)
MessageBox.Show("aaaa" + Environment.NewLine + "bbbb");
(4)
线程使用举例:
类里面定义了一个私有线程
 //线程,用于接收报文
        //线程,用于接收报文
 Thread t_ListenPort1;
在窗体的Load事件写入如下代码:
        Thread t_ListenPort1;
在窗体的Load事件写入如下代码:
 t_ListenPort1=new Thread(new ThreadStart(ListenPort));
 t_ListenPort1=new Thread(new ThreadStart(ListenPort));
 t_ListenPort1.Start();
其中ListenPort的定义如下:
                t_ListenPort1.Start();
其中ListenPort的定义如下:
 /// <summary>
    /// <summary>
 /// 开始一个线程 接收报文 发送抱文由Click事件决定
        /// 开始一个线程 接收报文 发送抱文由Click事件决定
 /// </summary>
        /// </summary>
 private  void ListenPort()
        private  void ListenPort()
 {
        {
 //
            //
 while (true)
            while (true)
 {
            {
 try
                try
 {
                {
 Random rdm_Message=new Random();
                    Random rdm_Message=new Random();
 int i_Messagetype=rdm_Message.Next(0,2);
                    int i_Messagetype=rdm_Message.Next(0,2);
 switch (i_Messagetype)
                    switch (i_Messagetype)
 {
                    {
 case 0:
                        case 0:
 //通行异常信息
                            //通行异常信息
 Recive_BLCM02();
                            Recive_BLCM02();
 break;
                            break;
 case 1:
                        case 1:
 //重新验证结果
                            //重新验证结果
 Recive_BLCR02();
                            Recive_BLCR02();
 break;
                            break;            
 }
                    }
 //暂停一段时间
                    //暂停一段时间                    
 Thread.Sleep(5000);
                    Thread.Sleep(5000); 
 }
                }
 catch
                catch
 {
                {

 }
                }            
 }
            }
 
          
 }
可以自己来写方法Recive_BLCM02()和Recive_BLCR02()
        }
可以自己来写方法Recive_BLCM02()和Recive_BLCR02()
(5)
读取配置文件指定的信息
配置文件App.Config格式如下:
 <configuration>
<configuration>
 <!-- 错误捕捉日志-->
    <!-- 错误捕捉日志-->
 <configSections>
    <configSections>
 <section name="exceptionManagement" type="OceanSoft.DevFramework.Common.ExceptionManagement.ExceptionManagerSectionHandler,OceanSoft.DevFramework.Common" />
        <section name="exceptionManagement" type="OceanSoft.DevFramework.Common.ExceptionManagement.ExceptionManagerSectionHandler,OceanSoft.DevFramework.Common" />
 </configSections>
    </configSections>
 <!--mode="on"记录异常信息mode="off"不记录异常信息-->
    <!--mode="on"记录异常信息mode="off"不记录异常信息-->
 <exceptionManagement mode="on">
    <exceptionManagement mode="on">
 <publisher assembly="OceanSoft.DevFramework.Common" type="OceanSoft.DevFramework.Common.ExceptionManagement.ExceptionPublisher" exclude="*" include="OceanSoft.DevFramework.Common.MyException, OceanSoft.DevFramework.Common" />
        <publisher assembly="OceanSoft.DevFramework.Common" type="OceanSoft.DevFramework.Common.ExceptionManagement.ExceptionPublisher" exclude="*" include="OceanSoft.DevFramework.Common.MyException, OceanSoft.DevFramework.Common" />
 </exceptionManagement>
    </exceptionManagement>
 <startup>
    <startup>
 <supportedRuntime version="v1.1.4322" />
        <supportedRuntime version="v1.1.4322" />
 <requiredRuntime version="v1.1.4322" safemode="true" />
        <requiredRuntime version="v1.1.4322" safemode="true" />
 </startup>
    </startup>
 <appSettings>
    <appSettings>
 <add key="StationProgram" value="D:\Program Files\Eclipse\eclipse.exe" />
        <add key="StationProgram" value="D:\Program Files\Eclipse\eclipse.exe" />
 <add key="barMainToolbar.Visible" value="True" />
        <add key="barMainToolbar.Visible" value="True" />
 <add key="FTPHost" value="172.17.100.85" />
        <add key="FTPHost" value="172.17.100.85" />
 <add key="FTPPort" value="21" />
        <add key="FTPPort" value="21" />
 <add key="FTPUser" value="logerp" />
        <add key="FTPUser" value="logerp" />
 <add key="FtpPass" value="logerp@oceansoft" />
        <add key="FtpPass" value="logerp@oceansoft" />
 <add key="SenderID" value="0003713" />
        <add key="SenderID" value="0003713" />
 <add key="ReceiverID" value="0003714" />
        <add key="ReceiverID" value="0003714" />
 <add key="BLC302Dir" value="BLC302/" />
        <add key="BLC302Dir" value="BLC302/" />
 <add key="BLC304Dir" value="BLC304/" />
        <add key="BLC304Dir" value="BLC304/" />
 <add key="BLC305Dir" value="BLC305/" />
        <add key="BLC305Dir" value="BLC305/" />
 <add key="BLC306Dir" value="BLC306/" />
        <add key="BLC306Dir" value="BLC306/" />
 <!--异常监控通道1-->
        <!--异常监控通道1-->
 <add key="Channel1" value="0001" />
        <add key="Channel1" value="0001" />
 <!--异常监控通道2-->
        <!--异常监控通道2-->
 <add key="Channel2" value="0002" />
        <add key="Channel2" value="0002" />
 <!--设备控制窗体刷新时间间隔 单位分钟-->
        <!--设备控制窗体刷新时间间隔 单位分钟-->
 <add key="TimeInterval" value="5"/>
        <add key="TimeInterval" value="5"/>
 </appSettings>
    </appSettings>
 </configuration>
首先使用引用,如下:
</configuration>
首先使用引用,如下:
 using System.Configuration;
读取相关配置信息如下:
using System.Configuration;
读取相关配置信息如下:
 //根据通道编号决定显示位置
    //根据通道编号决定显示位置
 string str_channel1 = ConfigurationSettings.AppSettings["channel1"];
                string str_channel1 = ConfigurationSettings.AppSettings["channel1"];
 string str_channel2 = ConfigurationSettings.AppSettings["channel2"];
                string str_channel2 = ConfigurationSettings.AppSettings["channel2"];
 (6)
(6)
根据线程当前状态关闭线程
 //窗体关闭的时候关闭线程
    //窗体关闭的时候关闭线程
 if (t_ListenPort1.ThreadState == ThreadState.Running)
            if (t_ListenPort1.ThreadState == ThreadState.Running)
 {
            {
 t_ListenPort1.Abort();
                t_ListenPort1.Abort();
 }
窗体关闭的时候线程关闭
            }
窗体关闭的时候线程关闭
 foreach(Thread t in threadHolder.Values)
    foreach(Thread t in threadHolder.Values)
 {  if(t != null && t.IsAlive)
            {  if(t != null && t.IsAlive)
 t.Abort();
                   t.Abort();
 }
            }
 Form1.ActiveForm.Close();
            Form1.ActiveForm.Close();    
(7)
对已经存在的DataTable 里面新增加列
 Condition pobj_Con = new Condition();
    Condition pobj_Con = new Condition();
 pobj_Con.Add("SQL", str_Sql);
            pobj_Con.Add("SQL", str_Sql);
 DataTable dt = this.Search("LOGERP_ASSET_Asset_SEL_20050830124902", pobj_Con).Tables[0];
            DataTable dt = this.Search("LOGERP_ASSET_Asset_SEL_20050830124902", pobj_Con).Tables[0];
 //this.Alert(dt.Rows.Count.ToString());
            //this.Alert(dt.Rows.Count.ToString());
 
                                    
 
                                    
 DataColumn ChannelState = new DataColumn();
            DataColumn ChannelState = new DataColumn();
 ChannelState.DataType = System.Type.GetType("System.String");
            ChannelState.DataType = System.Type.GetType("System.String");
 ChannelState.ColumnName = "ChannelState";
            ChannelState.ColumnName = "ChannelState";
 ChannelState.DefaultValue = "";
            ChannelState.DefaultValue = "";
 dt.Columns.Add(ChannelState);
最重要的是最后的5行
            dt.Columns.Add(ChannelState);
最重要的是最后的5行
显示日期
 DateTime.Now.ToString("yyyyMMdd")
DateTime.Now.ToString("yyyyMMdd")显示时间
 DateTime.Now.ToString("hhmmssff")
DateTime.Now.ToString("hhmmssff")(2)
使用枚举
 enum IC_CardState{unuse=0,natural,lose,freeze,logout,BeforeDate}
enum IC_CardState{unuse=0,natural,lose,freeze,logout,BeforeDate} if ((IC_CardType)dr_Result["CardType"]!=IC_CardType.fixCard)
if ((IC_CardType)dr_Result["CardType"]!=IC_CardType.fixCard) {
                { if (System.DateTime.Now>=Convert.ToDateTime(dr_Result["EndDate"]))
                    if (System.DateTime.Now>=Convert.ToDateTime(dr_Result["EndDate"])) {
                    { dr_Result["AfterStatus"]=(int)IC_CardState.BeforeDate; //过期卡
                        dr_Result["AfterStatus"]=(int)IC_CardState.BeforeDate; //过期卡 }
                    } }
                }(3)
字符串换行
 MessageBox.Show("aaaa" + Environment.NewLine + "bbbb");
MessageBox.Show("aaaa" + Environment.NewLine + "bbbb");线程使用举例:
类里面定义了一个私有线程
 //线程,用于接收报文
        //线程,用于接收报文 Thread t_ListenPort1;
        Thread t_ListenPort1; t_ListenPort1=new Thread(new ThreadStart(ListenPort));
 t_ListenPort1=new Thread(new ThreadStart(ListenPort)); t_ListenPort1.Start();
                t_ListenPort1.Start(); /// <summary>
    /// <summary> /// 开始一个线程 接收报文 发送抱文由Click事件决定
        /// 开始一个线程 接收报文 发送抱文由Click事件决定 /// </summary>
        /// </summary> private  void ListenPort()
        private  void ListenPort() {
        { //
            // while (true)
            while (true) {
            { try
                try {
                { Random rdm_Message=new Random();
                    Random rdm_Message=new Random(); int i_Messagetype=rdm_Message.Next(0,2);
                    int i_Messagetype=rdm_Message.Next(0,2); switch (i_Messagetype)
                    switch (i_Messagetype) {
                    { case 0:
                        case 0: //通行异常信息
                            //通行异常信息 Recive_BLCM02();
                            Recive_BLCM02(); break;
                            break; case 1:
                        case 1: //重新验证结果
                            //重新验证结果 Recive_BLCR02();
                            Recive_BLCR02(); break;
                            break;             }
                    } //暂停一段时间
                    //暂停一段时间                     Thread.Sleep(5000);
                    Thread.Sleep(5000);  }
                } catch
                catch {
                {
 }
                }             }
            } 
           }
        }(5)
读取配置文件指定的信息
配置文件App.Config格式如下:
 <configuration>
<configuration> <!-- 错误捕捉日志-->
    <!-- 错误捕捉日志--> <configSections>
    <configSections> <section name="exceptionManagement" type="OceanSoft.DevFramework.Common.ExceptionManagement.ExceptionManagerSectionHandler,OceanSoft.DevFramework.Common" />
        <section name="exceptionManagement" type="OceanSoft.DevFramework.Common.ExceptionManagement.ExceptionManagerSectionHandler,OceanSoft.DevFramework.Common" /> </configSections>
    </configSections> <!--mode="on"记录异常信息mode="off"不记录异常信息-->
    <!--mode="on"记录异常信息mode="off"不记录异常信息--> <exceptionManagement mode="on">
    <exceptionManagement mode="on"> <publisher assembly="OceanSoft.DevFramework.Common" type="OceanSoft.DevFramework.Common.ExceptionManagement.ExceptionPublisher" exclude="*" include="OceanSoft.DevFramework.Common.MyException, OceanSoft.DevFramework.Common" />
        <publisher assembly="OceanSoft.DevFramework.Common" type="OceanSoft.DevFramework.Common.ExceptionManagement.ExceptionPublisher" exclude="*" include="OceanSoft.DevFramework.Common.MyException, OceanSoft.DevFramework.Common" /> </exceptionManagement>
    </exceptionManagement> <startup>
    <startup> <supportedRuntime version="v1.1.4322" />
        <supportedRuntime version="v1.1.4322" /> <requiredRuntime version="v1.1.4322" safemode="true" />
        <requiredRuntime version="v1.1.4322" safemode="true" /> </startup>
    </startup> <appSettings>
    <appSettings> <add key="StationProgram" value="D:\Program Files\Eclipse\eclipse.exe" />
        <add key="StationProgram" value="D:\Program Files\Eclipse\eclipse.exe" /> <add key="barMainToolbar.Visible" value="True" />
        <add key="barMainToolbar.Visible" value="True" /> <add key="FTPHost" value="172.17.100.85" />
        <add key="FTPHost" value="172.17.100.85" /> <add key="FTPPort" value="21" />
        <add key="FTPPort" value="21" /> <add key="FTPUser" value="logerp" />
        <add key="FTPUser" value="logerp" /> <add key="FtpPass" value="logerp@oceansoft" />
        <add key="FtpPass" value="logerp@oceansoft" /> <add key="SenderID" value="0003713" />
        <add key="SenderID" value="0003713" /> <add key="ReceiverID" value="0003714" />
        <add key="ReceiverID" value="0003714" /> <add key="BLC302Dir" value="BLC302/" />
        <add key="BLC302Dir" value="BLC302/" /> <add key="BLC304Dir" value="BLC304/" />
        <add key="BLC304Dir" value="BLC304/" /> <add key="BLC305Dir" value="BLC305/" />
        <add key="BLC305Dir" value="BLC305/" /> <add key="BLC306Dir" value="BLC306/" />
        <add key="BLC306Dir" value="BLC306/" /> <!--异常监控通道1-->
        <!--异常监控通道1--> <add key="Channel1" value="0001" />
        <add key="Channel1" value="0001" /> <!--异常监控通道2-->
        <!--异常监控通道2--> <add key="Channel2" value="0002" />
        <add key="Channel2" value="0002" /> <!--设备控制窗体刷新时间间隔 单位分钟-->
        <!--设备控制窗体刷新时间间隔 单位分钟--> <add key="TimeInterval" value="5"/>
        <add key="TimeInterval" value="5"/> </appSettings>
    </appSettings> </configuration>
</configuration> using System.Configuration;
using System.Configuration; //根据通道编号决定显示位置
    //根据通道编号决定显示位置 string str_channel1 = ConfigurationSettings.AppSettings["channel1"];
                string str_channel1 = ConfigurationSettings.AppSettings["channel1"]; string str_channel2 = ConfigurationSettings.AppSettings["channel2"];
                string str_channel2 = ConfigurationSettings.AppSettings["channel2"];
根据线程当前状态关闭线程
 //窗体关闭的时候关闭线程
    //窗体关闭的时候关闭线程 if (t_ListenPort1.ThreadState == ThreadState.Running)
            if (t_ListenPort1.ThreadState == ThreadState.Running) {
            { t_ListenPort1.Abort();
                t_ListenPort1.Abort(); }
            } foreach(Thread t in threadHolder.Values)
    foreach(Thread t in threadHolder.Values) {  if(t != null && t.IsAlive)
            {  if(t != null && t.IsAlive) t.Abort();
                   t.Abort(); }
            } Form1.ActiveForm.Close();
            Form1.ActiveForm.Close();    (7)
对已经存在的DataTable 里面新增加列
 Condition pobj_Con = new Condition();
    Condition pobj_Con = new Condition(); pobj_Con.Add("SQL", str_Sql);
            pobj_Con.Add("SQL", str_Sql); DataTable dt = this.Search("LOGERP_ASSET_Asset_SEL_20050830124902", pobj_Con).Tables[0];
            DataTable dt = this.Search("LOGERP_ASSET_Asset_SEL_20050830124902", pobj_Con).Tables[0]; //this.Alert(dt.Rows.Count.ToString());
            //this.Alert(dt.Rows.Count.ToString()); 
                                     
                                     DataColumn ChannelState = new DataColumn();
            DataColumn ChannelState = new DataColumn(); ChannelState.DataType = System.Type.GetType("System.String");
            ChannelState.DataType = System.Type.GetType("System.String"); ChannelState.ColumnName = "ChannelState";
            ChannelState.ColumnName = "ChannelState"; ChannelState.DefaultValue = "";
            ChannelState.DefaultValue = ""; dt.Columns.Add(ChannelState);
            dt.Columns.Add(ChannelState); 
                    
                

 
                     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号