Beispiel #1
0
 private void button8_Click(object sender, EventArgs e)
 {
     if (openState == "0")
     {
         Thread sendOrderThread = new Thread(SendOrder);
         ComOrder tempcomOrder = new ComOrder();
         tempcomOrder.customFuncNum = showCOMNum;
         tempcomOrder.order = "Restart";
         sendOrderThread.Start((object)tempcomOrder);
     }
 }
Beispiel #2
0
 private void 重启ToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (openState == "0")
     {
         Thread sendOrderThread = new Thread(SendOrder);
         ComOrder tempcomOrder = new ComOrder();
         tempcomOrder.customFuncNum = 1000;
         tempcomOrder.order = "Restart";
         sendOrderThread.Start((object)tempcomOrder);
     }
     else if (openState == "Open failure")
     {
         FirstRun();
     }
     else
     {
     }
 }
Beispiel #3
0
 private void 读取配置ToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (openState == "0")
     {
         Thread sendOrderThread = new Thread(SendOrder);
         ComOrder tempcomOrder = new ComOrder();
         tempcomOrder.customFuncNum = 1000;
         tempcomOrder.order = "ReadSetup";
         sendOrderThread.Start((object)tempcomOrder);
     }
 }
Beispiel #4
0
        public void MainRun()
        {
            try
            {
                bool createdNew;
                SyncNamed = new Mutex(false, MutexName,out createdNew);
                if (!createdNew)
                {
                    LogHelper.WriteLog("创建同步对象意外失败,服务准备停止。");
                    this.OnStop();
                }

                var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
                var memoryMapSecurity = new MemoryMappedFileSecurity();
                memoryMapSecurity.AddAccessRule(new AccessRule<MemoryMappedFileRights>(everyone, MemoryMappedFileRights.ReadWrite, AccessControlType.Allow));
                LogHelper.WriteLog("设置内存权限....");
                LogHelper.WriteLog("创建comMMF内存...");
                MemoryMappedFile comMMF = MemoryMappedFile.CreateOrOpen("Global\\funcMMF", capacity, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, memoryMapSecurity, System.IO.HandleInheritability.None);      
                LogHelper.WriteLog("创建comOrderMMF内存....");

                MemoryMappedFile comOrderMMF = MemoryMappedFile.CreateOrOpen("Global\\funcOrderMMF", capacity, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, memoryMapSecurity, System.IO.HandleInheritability.None);                
                //通过MemoryMappedFile的CreateViewAccssor方法获得共享内存的访问器
                MemoryMappedViewAccessor comMMFViewAccessor;
                //循环写入,使在这个进程中可以向共享内存中写入不同的字符串值
                MemoryMappedViewAccessor comOrderMMFViewAccessor;
                comMMFViewAccessor = comMMF.CreateViewAccessor(0, capacity);
                comOrderMMFViewAccessor = comOrderMMF.CreateViewAccessor(0, capacity);
                string input;
                int strLength;
                char[] charsInMMf;
                //----------------------------------------------------------------
                input = XmlSerialize.SerializeXML<ComOrder>(a_comOrder);
                //向共享内存开始位置写入字符串的长度
                comOrderMMFViewAccessor.Write(0, input.Length);
                //向共享内存4位置写入字符
                comOrderMMFViewAccessor.WriteArray<char>(4, input.ToArray(), 0, input.Length);
                //----------------------------------------------------------------
                while (stopFlag=="Run")
                {
                    try
                    {      
                        SyncNamed.WaitOne();

                        input = XmlSerialize.SerializeXML<List<CustomFunc>>(CustomFunc);
                        //向共享内存开始位置写入字符串的长度
                        comMMFViewAccessor.Write(0, input.Length);
                        //向共享内存4位置写入字符
                        comMMFViewAccessor.WriteArray<char>(4, input.ToArray(), 0, input.Length);

                        //------------------------------释放同步对象
                        SyncNamed.ReleaseMutex();
                        //------------------------------

                        //-------------------------------------------------------------------------------
                        strLength = comOrderMMFViewAccessor.ReadInt32(0);
                        charsInMMf = new char[strLength];
                        //读取字符
                        comOrderMMFViewAccessor.ReadArray<char>(4, charsInMMf, 0, strLength);
                        a_comOrder = XmlSerialize.DeserializeXML<ComOrder>(new string(charsInMMf));
                        if (a_comOrder != null)
                        {
                            if (a_comOrder.order != a_comOrder2.order || a_comOrder.customFuncNum != a_comOrder2.customFuncNum)
                            {
                                a_comOrder2.order = a_comOrder.order;
                                a_comOrder2.customFuncNum = a_comOrder.customFuncNum;
                                LogHelper.WriteLog("执行命令...串口设备" + a_comOrder.customFuncNum.ToString() + "-" + a_comOrder.order);
                                if (a_comOrder.customFuncNum == 1000)
                                {
                                    if (a_comOrder.order == "Start")
                                    {
                                        foreach (CustomFunc m_CustomFunc in CustomFunc)
                                        {
                                            ParameterizedThreadStart ParStart = new ParameterizedThreadStart(m_CustomFunc.VoidRunOrder);
                                            Thread tempThread = new Thread(ParStart);
                                            tempThread.IsBackground = true;
                                            tempThread.Start((object)"Start");
                                        }
                                    }
                                    else if (a_comOrder.order == "Stop")
                                    {
                                        foreach (CustomFunc m_CustomFunc in CustomFunc)
                                        {
                                            ParameterizedThreadStart ParStart = new ParameterizedThreadStart(m_CustomFunc.VoidRunOrder);
                                            Thread tempThread = new Thread(ParStart);
                                            tempThread.IsBackground = true;
                                            tempThread.Start((object)"Stop");
                                        }
                                    }
                                    else if (a_comOrder.order == "ForceStop")
                                    {
                                        foreach (CustomFunc m_CustomFunc in CustomFunc)
                                        {
                                            ParameterizedThreadStart ParStart = new ParameterizedThreadStart(m_CustomFunc.VoidRunOrder);
                                            Thread tempThread = new Thread(ParStart);
                                            tempThread.IsBackground = true;
                                            tempThread.Start((object)"ForceStop");
                                        }
                                    }
                                    else if (a_comOrder.order == "Restart")
                                    {
                                        foreach (CustomFunc m_CustomFunc in CustomFunc)
                                        {
                                            ParameterizedThreadStart ParStart = new ParameterizedThreadStart(m_CustomFunc.VoidRunOrder);
                                            Thread tempThread = new Thread(ParStart);
                                            tempThread.IsBackground = true;
                                            tempThread.Start((object)"Restart"); ;
                                        }
                                    }
                                    else if (a_comOrder.order == "ReadSetup")
                                    {
                                        int tempEndState = 0;
                                        foreach (CustomFunc m_CustomFunc in CustomFunc)
                                        {
                                            if (m_CustomFunc.a_funcInfo.a_stateValue.runState == "End")
                                            {
                                                tempEndState += 1;
                                            };
                                        }
                                        if (tempEndState == CustomFunc.Count)
                                        {
                                            LoadSetup(); ;
                                        }
                                        else
                                        {
                                            string tempLogString;
                                            tempLogString = "检测到还有串口未停止,读取配置中止";
                                            LogHelper.WriteLog(tempLogString);
                                        }
                                    }
                                    else
                                    {
                                    }
                                }
                                else
                                {
                                    if (a_comOrder.customFuncNum <= CustomFunc.Count - 1)
                                    {
                                        if (a_comOrder.order == "Start")
                                        {
                                            ParameterizedThreadStart ParStart = new ParameterizedThreadStart(CustomFunc[a_comOrder.customFuncNum].VoidRunOrder);
                                            Thread tempThread = new Thread(ParStart);
                                            tempThread.IsBackground = true;
                                            tempThread.Start((object)"Start");
                                        }
                                        else if (a_comOrder.order == "Stop")
                                        {
                                            ParameterizedThreadStart ParStart = new ParameterizedThreadStart(CustomFunc[a_comOrder.customFuncNum].VoidRunOrder);
                                            Thread tempThread = new Thread(ParStart);
                                            tempThread.IsBackground = true;
                                            tempThread.Start((object)"Stop");
                                        }
                                        else if (a_comOrder.order == "ForceStop")
                                        {
                                            ParameterizedThreadStart ParStart = new ParameterizedThreadStart(CustomFunc[a_comOrder.customFuncNum].VoidRunOrder);
                                            Thread tempThread = new Thread(ParStart);
                                            tempThread.IsBackground = true;
                                            tempThread.Start((object)"ForceStop");
                                        }
                                        else if (a_comOrder.order == "Restart")
                                        {
                                            ParameterizedThreadStart ParStart = new ParameterizedThreadStart(CustomFunc[a_comOrder.customFuncNum].VoidRunOrder);
                                            Thread tempThread = new Thread(ParStart);
                                            tempThread.IsBackground = true;
                                            tempThread.Start((object)"Restart");
                                        }
                                        else
                                        {
                                        }
                                    }
                                    else
                                    {
                                        string tempLogString;
                                        tempLogString = "指定设备" + a_comOrder.customFuncNum + "超出实际设备数量 " + CustomFunc.Count + ",指令中止。";
                                        LogHelper.WriteLog(tempLogString);
                                    }
                                }
                            }
                        }
                        else
                        {
                            LogHelper.WriteLog("读取内存中指令出错");
                        }
                    }
                    catch (WaitHandleCannotBeOpenedException)
                    {
                        LogHelper.WriteLog("打开同步对象意外失败");
                    }
                    finally
                    {
#if DEBUG
                    Debug.Write(a_comOrder.order);
#endif
                        Thread.Sleep(new TimeSpan(0, 0, 0, 0, 1000));
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex as Exception);
            }
        }