www.pudn.com > RAPIAutoTransfer.zip > MainForm.cs, change:2014-12-26,size:7093b


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Threading; 
using System.IO; 
 
namespace RAPIAutoTransfer 
{ 
    public partial class MainForm : Form 
    { 
        private static log4net.ILog log = log4net.LogManager.GetLogger("WinCERapiTransfer"); 
        public Gicom.Rapi.RAPI rapi; 
        public string localFile; 
        public string remoteFile; 
        private static Thread rapiThread = null; 
        private delegate void ShowInfoDelegate(string info); 
        private delegate void EnableControlsDelegate(bool enable); 
        private string outwincePath = @"Application\ToolsManagement\Outbox"; 
        private string inwincePath = @"Application\ToolsManagement\Inbox"; 
        private string outExportPath = @"C:\GICOM\Outbox"; 
        private string inExportPath = @"C:\GICOM\Inbox"; 
 
        public MainForm() 
        { 
            InitializeComponent(); 
        } 
 
        private void MainForm_Load(object sender, EventArgs e) 
        { 
            Init(); 
        } 
        private void showInfo(string info) 
        { 
            if (this.InvokeRequired) 
            { 
                this.Invoke(new ShowInfoDelegate(showInfo), new object[] { info }); 
                return; 
            } 
            this.textBoxInfo.AppendText(info + "\r\n"); 
            this.textBoxInfo.ScrollToCaret(); 
        } 
 
        private void enableControls(bool enable) 
        { 
            if (this.InvokeRequired) 
            { 
                this.Invoke(new EnableControlsDelegate(enableControls), new object[] { enable }); 
                return; 
            } 
            this.buttonStart.Enabled = enable; 
            this.buttonStop.Enabled = !enable; 
        } 
 
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e) 
        { 
            e.Cancel = true; 
            this.Hide(); 
            this.notifyIcon1.Visible = true; 
        } 
 
        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) 
        { 
            MouseEventArgs Mouse_e = (MouseEventArgs)e; 
            if (Mouse_e.Button == MouseButtons.Left) 
            { 
                this.Visible = true; 
                this.WindowState = System.Windows.Forms.FormWindowState.Normal; 
            } 
        } 
 
        private void ToolStripMenuItemShow_Click(object sender, EventArgs e) 
        { 
            this.Visible = true; 
            this.WindowState = System.Windows.Forms.FormWindowState.Normal; 
        } 
 
        private void ToolStripMenuItemExit_Click(object sender, EventArgs e) 
        { 
            myExit(); 
        } 
 
        private void myExit() 
        { 
            try 
            { 
                this.notifyIcon1.Visible = false; 
                this.notifyIcon1.Dispose(); 
                System.Environment.Exit(0); 
            } 
            catch { } 
        } 
 
        /// <summary> 
        /// 启动信息传送线程 
        /// </summary> 
        public void Init() 
        { 
            try 
            { 
                rapiThread = new Thread(OperatThread); 
                rapiThread.Name = "WinCERapiTransfer"; 
                rapiThread.Priority = ThreadPriority.Lowest; 
                rapiThread.Start(); 
                showInfo("\r\n信息传送线程启动"); 
                enableControls(false); 
            } 
            catch (Exception ex) 
            { 
                log.Error("启动信息传送线程异常:" + ex.Message); 
                showInfo("启动信息传送线程异常:" + ex.Message); 
            } 
        } 
        /// <summary> 
        /// 终止线程 
        /// </summary> 
        public void Release() 
        { 
            if (rapiThread!=null) 
            { 
                rapiThread.Abort(); 
                rapiThread = null; 
                showInfo("\r\n信息传送线程停止"); 
            } 
 
        } 
        /// <summary> 
        /// 设置线程休眠时间 
        /// </summary> 
        public void Sleep() 
        { 
            Thread.Sleep(100); 
        } 
 
        public void OperatThread() 
        { 
            try 
            { 
                while (true) 
                { 
                    try 
                    { 
                        Operations(); 
                        Sleep(); 
                    } 
                    catch (Exception ex) 
                    { 
                        log.Debug("rapi处理异常:" + ex.Message); 
                        break; 
                    } 
                } 
            } 
            catch (Exception ex) 
            { 
                log.Debug("rapi侦听异常:" + ex.Message); 
            } 
        } 
 
        /// <summary> 
        /// 业务处理 
        /// </summary> 
        private void Operations() 
        { 
            try 
            { 
                rapi = new Gicom.Rapi.RAPI(); 
                rapi.Connect(false, -1); 
                if (!rapi.DevicePresent) 
                { 
                    return; 
                } 
                if (!Directory.Exists(outExportPath)) 
                { 
                    Directory.CreateDirectory(outExportPath); 
                } 
                string outFile = string.Format("{0}\\{1}", outExportPath, "out.txt"); 
                remoteFile = string.Format("{0}\\{1}", outwincePath, "out.txt"); 
                try 
                { 
                    RAPIFileTransfer aRAPIFileTransfer = new RAPIFileTransfer(rapi, outFile, remoteFile); 
                    aRAPIFileTransfer.Receive(); 
                    showInfo("工具借出数据导出成功!"); 
                    rapi.DeleteDeviceFile(remoteFile); 
                } 
                catch 
                { 
                } 
                
                if (!Directory.Exists(inExportPath)) 
                { 
                    Directory.CreateDirectory(inExportPath); 
                } 
                string inFile = string.Format("{0}\\{1}", inExportPath, "in.txt"); 
                remoteFile = string.Format("{0}\\{1}", inwincePath, "in.txt"); 
                try 
                { 
                    RAPIFileTransfer aRAPIFileTransfer = new RAPIFileTransfer(rapi, inFile, remoteFile); 
                    aRAPIFileTransfer.Receive(); 
                    showInfo("工具归还数据导出成功!"); 
                    rapi.DeleteDeviceFile(remoteFile); 
                } 
                catch 
                { 
                } 
            } 
            catch (Exception ex) 
            { 
                log.Error(ex.Message); 
            } 
        } 
 
        private void buttonStop_Click(object sender, EventArgs e) 
        { 
            Release(); 
            enableControls(true); 
        } 
 
        private void buttonStart_Click(object sender, EventArgs e) 
        { 
            Init(); 
        } 
    } 
}