using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.IO.Ports; using System.Linq; using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using BO; using BO.Utils; using BWP.WinFormControl.WeightDataFormat; using Forks.JsonRpc.Client; using MaterialRequisition.Model; using Newtonsoft.Json; namespace MaterialRequisition { public partial class MaterialRequisitionForm : Form,IAfterLogin { public string RoleName { get { return "领料退料"; } } public Form Generate() { return this; } private readonly string mDropDownSetsFileName = "MaterialRequisition_DropDownSets.xml"; private DropDownSets mDropDownSets; #region weightNeed SerialPort weightPort; private IDataFormat _dataFormat; private Thread _inQueryThread; private bool _mainProcessIsRun; readonly StringBuilder _dataStrBuilder = new StringBuilder(); #endregion private readonly Thread _tdSyncLocalToMiddleDb; private readonly Thread _tcCheckNetStatus; private readonly Thread _tcCheckSyncStatus; public MaterialRequisitionForm() { InitializeComponent(); dataGridViewUnSubmit.AutoGenerateColumns = false; dataGridViewSubmited.AutoGenerateColumns = false; weightPort = new SerialPort(); this.FormClosing += delegate { if (_inQueryThread != null && _inQueryThread.IsAlive) { //DisableWeight(); } if (_tdSyncLocalToMiddleDb != null && _tdSyncLocalToMiddleDb.IsAlive) { _tdSyncLocalToMiddleDb.Abort(); } if (_tcCheckNetStatus != null && _tcCheckNetStatus.IsAlive) { _tcCheckNetStatus.Abort(); } if (_tcCheckSyncStatus != null && _tcCheckSyncStatus.IsAlive) { _tcCheckSyncStatus.Abort(); } }; InitCombox(); _tdSyncLocalToMiddleDb = new Thread(SyncLocalToMiddleDb); _tdSyncLocalToMiddleDb.Start(); _tcCheckNetStatus = new Thread(CheckNetStatus); _tcCheckNetStatus.Start(); _tcCheckSyncStatus = new Thread(CheckSyncStatus); _tcCheckSyncStatus.Start(); } private void CheckSyncStatus() { while (true) { var syncSuccessed = MaterialRequisitionRecordRpc.GetInstance().IsSyncSucessed(); var png = "stop.png"; if (syncSuccessed) png = "working.png"; var imgPath = Path.Combine(Application.StartupPath, "BWP.WinFormControl.dll"); var s = Assembly.LoadFile(imgPath).GetManifestResourceStream("BWP.WinFormControl.Images." + png); if (this.InvokeRequired) { this.BeginInvoke(new Action(() => { picSyncStatus.Image = Image.FromStream(s); picSyncStatus.Refresh(); })); } else { picSyncStatus.Image = Image.FromStream(s); picSyncStatus.Refresh(); } Thread.Sleep(1000); } } private bool laseConnection = false; private void CheckNetStatus() { while (true) { try { var newConnection = LoginRpcUtil.TestConnection(500); if (newConnection && laseConnection) { Thread.Sleep(1000); continue; } var png = "stop.png"; if (newConnection) png = "working.png"; var imgPath = Path.Combine(Application.StartupPath, "BWP.WinFormControl.dll"); var s = Assembly.LoadFile(imgPath).GetManifestResourceStream("BWP.WinFormControl.Images." + png); if (this.InvokeRequired) { this.BeginInvoke(new Action(() => { picNetStatus.Image = Image.FromStream(s); picNetStatus.Refresh(); })); } else { picNetStatus.Image = Image.FromStream(s); picNetStatus.Refresh(); } laseConnection = newConnection; } catch (Exception e) { //LogUtil.Error(e.ToString()); } Thread.Sleep(1000); } } private void SyncLocalToMiddleDb() { while (true) { if (laseConnection) { this.BeginInvoke(new Action(() => { MaterialRequisitionRecordRpc.GetInstance().SyncToServer(); })); } Thread.Sleep(1000); } } private void InitCombox() { if (LoginRpcUtil.TestConnection(500)) { mDropDownSets = GetmDropDownSets(); XmlUtil.SerializerObjToFile(mDropDownSets, mDropDownSetsFileName); } else { mDropDownSets = XmlUtil.DeserializeFromFile(mDropDownSetsFileName); } var shop = mDropDownSets.Details.FirstOrDefault(x => x.Name == DropDownSets.车间); if (shop != null) { cbxWorkShop.DataSource = shop.Details; cbxWorkShop.DisplayMember = "Name"; cbxWorkShop.ValueMember = "ID"; } var unit = mDropDownSets.Details.FirstOrDefault(x => x.Name == DropDownSets.单元); if (unit != null) { cbxWorkUnit.DataSource = unit.Details; cbxWorkUnit.DisplayMember = "Name"; cbxWorkUnit.ValueMember = "Code"; } } private DropDownSets GetmDropDownSets() { var sets = new DropDownSets(); var wrokUnitSet = GetWrokUnitSet(); var wrokShopSet = GetWrokShopSet(); sets.Details.Add(wrokUnitSet); sets.Details.Add(wrokShopSet); return sets; } private DropDownSet GetWrokShopSet() { var json = RpcFacade.Call("/MainSystem/B3ClientService/Rpcs/BaseInfoRpc/GetWorkShopList"); var set = new DropDownSet(); set.Name = DropDownSets.车间; foreach (var detail in JsonConvert.DeserializeObject>(json)) { set.Details.Add(detail); } return set; } private DropDownSet GetWrokUnitSet() { var json = RpcFacade.Call("/MainSystem/B3ClientService/Rpcs/BaseInfoRpc/GetWorkUnitList"); var set = new DropDownSet(); set.Name = DropDownSets.单元; foreach (var detail in JsonConvert.DeserializeObject>(json)) { set.Details.Add(detail); } return set; } } }