Example #1
0
 //更新显示器
 private void UpdateMonitor(int quality)
 {
     if (this.label1.InvokeRequired)//等待异步
     {
         ThreadWork fc = new ThreadWork(UpdateMonitor);
         // this.Invoke(fc);//通过代理调用刷新方法
         this.Invoke(fc, new object[1] {
             quality
         });
     }
     else
     {
         label1.Text = quality.ToString();
         if (quality > 80)
         {
             label1.ForeColor = Color.Green;
         }
         else if (quality > 60)
         {
             label1.ForeColor = Color.Orange;
         }
         else if (quality > 40)
         {
             label1.ForeColor = Color.Yellow;
         }
         else
         {
             label1.ForeColor = Color.Red;
         }
     }
 }
Example #2
0
 private void ThreadFunction(int o)
 {
     if (this.lblMsg.InvokeRequired)//等待异步
     {
         ThreadWork fc = new ThreadWork(ThreadFunction);
         // this.Invoke(fc);//通过代理调用刷新方法
         this.Invoke(fc, new object[1] {
             o
         });
     }
     else
     {
         string diang = ".";
         int    i     = int.Parse(this.lblMsg.Tag.ToString());
         if (i <= 2)
         {
             i = i + 1;
         }
         else
         {
             i = 1;
         }
         diang            = diang.PadRight(i, '.');
         this.lblMsg.Tag  = i.ToString();
         this.lblMsg.Text = String.Format("正在打印{0}", diang);
         this.lblMsg.Refresh();
         Application.DoEvents();
     }
 }
Example #3
0
 private void setchart(Chart mychart, int machine_num)
 {
     // 针对一个图表和对应的机号的处理
     try
     {
         if (mychart.InvokeRequired)
         {
             ThreadWork tw = new ThreadWork(setchart);
             this.Invoke(tw, new object[2] {
                 mychart, machine_num
             });
         }
         else
         {
             mychart.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
             DataSet  ds        = mv.mysqlconnection.sql_search_database("select * from start_time where machine_num=" + machine_num.ToString());
             DataRow  dr        = ds.Tables[0].Rows[0];
             DateTime starttime = DateTime.Parse(dr[1].ToString());
             mychart.ChartAreas[0].AxisX.Minimum      = starttime.ToOADate();
             mychart.ChartAreas[0].AxisX.Maximum      = starttime.AddHours(5).ToOADate();
             mychart.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Hours;
             mychart.ChartAreas[0].AxisX.Interval     = 2;
         }
     }
     catch
     {
     }
 }
Example #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            count++;
            string     thread     = "Thread ";
            ThreadWork threadWork = new ThreadWork(thread + count.ToString(), count);

            bsCreated.Add(threadWork);
        }
Example #5
0
 private void AddThreadsToWorkingList()
 {
     if (bsWaiting.Count() > 0)// if the waiting list has any threads
     {
         ThreadWork threadWork = bsWaiting.First(x => x.Number == bsWaiting.Max(y => y.Number));
         bsWaiting.Remove(threadWork);
         bsWorking.Add(threadWork);
         //the semaphore is sent as a parameter to the ThreadWork class.
         threadWork.Start(_s);
     }
 }
Example #6
0
 /// <summary>
 ///  更新控件内容
 /// </summary>
 /// Author  : Napoleon
 /// Created : 2015-07-25 08:53:48
 private void ThreadFunction(string content)
 {
     if (RchExport.InvokeRequired)
     {
         ThreadWork work = ThreadFunction;
         Invoke(work, new object[] { content });
     }
     else
     {
         RchExport.AppendText(content);
         RchExport.Refresh();
     }
 }
Example #7
0
    public void QueueWork(ThreadWork task, object arg, bool isPriority)
    {
        if (!isPriority)
        {
            work.Enqueue(task);
        }
        else
        {
            priority.Enqueue(task);
        }

        data.Enqueue(arg);
    }
Example #8
0
 private void lbWorkingThreads_DoubleClick(object sender, EventArgs e)
 {
     if (lbWorkingThreads.SelectedItem != null)
     {
         ThreadWork workingThread = lbWorkingThreads.SelectedItem as ThreadWork;
         bsWorking.Remove(workingThread);
         workingThread.Stop();
     }
     // when the working list has places we add new threads from the Waiting list
     if (bsWorking.Count - (int)nUDNumber.Value < 0)
     {
         AddThreadsToWorkingList();
     }
 }
Example #9
0
 //发送
 private void ThreadReceiveFile(string msg)
 {
     if (this.tbReceiveMsg.InvokeRequired)//等待异步
     {
         ThreadWork fc = new ThreadWork(ThreadReceiveFile);
         // this.Invoke(fc);//通过代理调用刷新方法
         this.Invoke(fc, msg);
     }
     else
     {
         recFileCount++;
         this.tbFileReceive.Text += DateTime.Now.ToString("[HH:mm:ss:fff] ") + recCount + "    " + msg + "\r\n";
     }
 }
Example #10
0
 //接收
 private void ThreadSendFile(string msg)
 {
     if (this.tbSend.InvokeRequired)//等待异步
     {
         ThreadWork fc = new ThreadWork(ThreadSendFile);
         // this.Invoke(fc);//通过代理调用刷新方法
         this.Invoke(fc, msg);
     }
     else
     {
         sendFileCount++;
         this.tbFileSend.Text += DateTime.Now.ToString("[HH:mm:ss:fff] ") + sendCount + "    " + msg + "\r\n";
     }
 }
Example #11
0
    void _UpdateHandler(ThreadWork t)
    {
        _LastSetAngles = new int[Joints.Length];
        for (int i = 0; i < _LastSetAngles.Length; ++i)
        {
            _LastSetAngles [i] = 9999;
        }

        while (!t.IsDone)
        {
            List <VSidoProtocol.JointAnglePair> pairs = new List <VSidoProtocol.JointAnglePair>();
            for (int i = 0; i < Joints.Length; ++i)
            {
                short angle = Joints[i].GetAngleValue();
                if (angle != _LastSetAngles [i])
                {
                    VSidoProtocol.JointAnglePair p = new VSidoProtocol.JointAnglePair();
                    p.id    = (byte)Joints[i].ID;
                    p.angle = angle;
                    pairs.Add(p);
                    //newAngles[i]=angle;
                }
                _LastSetAngles [i] = angle;
            }
            byte[] buffer = _client.gen_cmd_set_objects(pairs.ToArray());          //=new byte[Joints.Length*8];

            /*int offset=0;
             *
             * for (int i=0; i<Joints.Length; ++i)
             * {
             *      if(newAngles[i]!=9999)
             *      {
             *              byte[] opcode = _client.gen_cmd_set_object( (byte)(Joints[i].ID),(short)(newAngles[i]));
             *              for(int j=0;j<opcode.Length;++j)
             *                      buffer[offset+j]=opcode[j];
             *              offset+=opcode.Length;
             *      }
             * }*/
            //if(offset>0)
            {
                try{
                    _serialPort.Write(buffer, 0, buffer.Length);
                }catch (Exception)
                {
                }
            }
            System.Threading.Thread.Sleep(30);
        }
    }
Example #12
0
        internal void StartPublishThread()
        {
            if (!IsCopy)
            {
                return;
            }
            var threadWork = new ThreadWork();

            threadWork.Start("OnSubscribeIEnumerable<T>", () =>
            {
                var ts = DateTime.Now - CacheDataTime;
                if (ts.TotalSeconds > 1)
                {
                    rePublish();
                }
                return(true);
            }, 0.5);
        }
Example #13
0
        private void sPort01_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] recvBuf = new byte[1024];
            int    l       = 0;

            while (sPort01.BytesToRead > 0)
            {
                l = sPort01.Read(recvBuf, 0, 1024);
                if (l > 0)
                {
                    AppendBuffer(data1, recvBuf, l);
                }
            }

            while (true)
            {
                String s = ReadStringFromStream(data1);

                if (s == null)
                {
                    break;
                }

                if (this.InvokeRequired)
                {
                    ThreadWork fc = new ThreadWork(showCOM01DataRecv);
                    this.Invoke(fc, s);
                }
                else
                {
                    showCOM01DataRecv(s);
                }

                lock (sPort02)
                {
                    if (sPort02.IsOpen)
                    {
                        sPort02.Write(s);
                    }
                }
            }

            ClearReadBuffer(data1);
        }
Example #14
0
 private void BalanceWorkingThreads()
 {
     if (previousNumber < (int)nUDNumber.Value)
     {
         if (bsWaiting.Count > 0)
         { // function that add threads to the working list
             AddThreadsToWorkingList();
         }
     }
     else
     {
         if (bsWorking.Count > 0)
         {
             // find max to remove it
             ThreadWork threadWork = bsWorking.First(x => x.Count == bsWorking.Max(y => y.Count));
             bsWorking.Remove(threadWork);
             threadWork.Stop();
         }
     }
     // for checking
     previousNumber = (int)nUDNumber.Value;
 }
Example #15
0
        public override void Main()
        {
            ProcessName = "Thread";
            var filmItems = GetRestItemsFilm();
            var threads   = new List <Thread>();

            for (int i = 0; i < filmItems.Count; i++)
            {
                string     uri        = filmItems[i];
                ThreadWork threadWork = new ThreadWork(uri);
                Thread     thread     = new Thread(new ThreadStart(threadWork.DoWork));
                threads.Add(thread);
                thread.Start();
                Thread.Sleep(10);
            }

            for (int i = 0; i < threads.Count; i++)
            {
                Thread thread = threads[i];
                thread.Join();
            }
        }
Example #16
0
 //接收
 private void ThreadSendFile(string msg)
 {
     if (this.tbSend.InvokeRequired)//等待异步 
     {
         ThreadWork fc = new ThreadWork(ThreadSendFile);
         // this.Invoke(fc);//通过代理调用刷新方法 
         this.Invoke(fc, msg);
     }
     else
     {
         sendFileCount++;
         this.tbFileSend.Text += DateTime.Now.ToString("[HH:mm:ss:fff] ") + sendCount + "    " + msg + "\r\n";
     }
 }
Example #17
0
        private void sPort02_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] recvBuf = new byte[1024];
            int    l       = 0;

            FileStream fWrite = null;

            if (bCOM02Record2File)
            {
                String sFile = this.GetType().Assembly.Location;
                sFile = System.IO.Path.GetFullPath(sFile) + "COM_01.DAT";

                fWrite = new FileStream(sFile, FileMode.Append);
            }

            while (sPort02.BytesToRead > 0)
            {
                l = sPort02.Read(recvBuf, 0, 1024);
                if (l > 0)
                {
                    AppendBuffer(data2, recvBuf, l);
                    if (fWrite != null)
                    {
                        fWrite.Write(recvBuf, 0, l);
                    }
                }
            }

            if (fWrite != null)
            {
                fWrite.Close();
            }



            while (true)
            {
                String s = ReadStringFromStream(data2);

                if (s == null)
                {
                    break;
                }

                if (this.InvokeRequired)
                {
                    ThreadWork fc = new ThreadWork(showCOM02DataRecv);
                    this.Invoke(fc, s);
                }
                else
                {
                    showCOM02DataRecv(s);
                }

                lock (sPort01)
                {
                    if (sPort01.IsOpen)
                    {
                        sPort01.Write(s);
                    }
                }
            }

            ClearReadBuffer(data2);

            //MemoryStream ms = new MemoryStream();
            //byte b = 0;

            //while (sPort02.BytesToRead > 0)
            //{
            //    b = (byte)sPort02.ReadByte();
            //    ms.WriteByte(b);
            //}

            //if (ms.Length > 0)
            //{
            //    ms.Position = 0;
            //    byte[] buf = new byte[ms.Length];
            //    ms.Read(buf, 0, buf.Length);

            //    lock (sPort01)
            //    {
            //        if (sPort01.IsOpen)
            //        {
            //            sPort01.Write(buf, 0, buf.Length);
            //        }
            //    }


            //    String s = System.Text.Encoding.Default.GetString(buf);

            //    if (this.InvokeRequired)
            //    {
            //        ThreadWork fc = new ThreadWork(showCOM02DataRecv);
            //        this.Invoke(fc, s);
            //    }
            //    else
            //    {
            //        showCOM02DataRecv(s);
            //    }
            //}
        }
Example #18
0
 //发送
 private void ThreadReceiveFile(string msg)
 {
     if (this.tbReceiveMsg.InvokeRequired)//等待异步 
     {
         ThreadWork fc = new ThreadWork(ThreadReceiveFile);
         // this.Invoke(fc);//通过代理调用刷新方法 
         this.Invoke(fc, msg);
     }
     else
     {
         recFileCount++;
         this.tbFileReceive.Text += DateTime.Now.ToString("[HH:mm:ss:fff] ") + recCount + "    " + msg + "\r\n";
     }
 }
Example #19
0
 public static void QueueWork(ThreadWork task, object arg, bool isPriority)
 {
     next = (next + 1) % threads.Length;
     threads[next].QueueWork(task, arg, isPriority);
 }
Example #20
0
 private void draw_chart(Chart mychart, int machine_num)
 {
     //将对应的开始时间读出来
     if (true)
     {
         if (mychart.InvokeRequired)
         {
             ThreadWork tw = new ThreadWork(draw_chart);
             try
             {
                 this.Invoke(tw, new object[2] {
                     mychart, machine_num
                 });
             }
             catch { }
         }
         else
         {
             try
             {
                 DataSet  ds        = mv.mysqlconnection.sql_search_database("Select * from start_time where machine_num=" + machine_num.ToString());
                 DataRow  dr        = ds.Tables[0].Rows[0];
                 DateTime starttime = DateTime.Parse(dr[1].ToString());
                 // 将历史记录中相相应的都读出来
                 ds = mv.mysqlconnection.sql_search_database("Select machine_num,value,save_time from history where machine_num=" + machine_num.ToString() + " and  save_time>" + "'" + starttime.ToString() + "' and save_time<'" + starttime.AddHours(5).ToString() + "' order by save_time");
                 int count = ds.Tables[0].Rows.Count;
                 mychart.Series[0].Points.Clear();
                 for (int i = 0; i < count; i++)
                 {
                     DataRow  mydr    = ds.Tables[0].Rows[i];
                     int      myvalue = int.Parse(mydr[1].ToString());
                     DateTime mytime  = DateTime.Parse(mydr[2].ToString());
                     mychart.Series[0].Points.AddXY(mytime, myvalue);
                 }
             }
             catch
             {
                 mychart.Series[0].Points.Clear();
             }
             // 画设定曲线
             try
             {
                 // int datagridview_row_count = dataGridView1.Rows.Count;
                 DataSet  ds        = mv.mysqlconnection.sql_search_database("Select * from start_time where machine_num=" + machine_num.ToString());
                 DataRow  dr        = ds.Tables[0].Rows[0];
                 DateTime starttime = DateTime.Parse(dr[1].ToString()); //开始时间
                 DataSet  gongyi_ds = mv.mysqlconnection.sql_search_database("Select * from craft_machine" + machine_num.ToString());
                 int      datagridview_row_count = gongyi_ds.Tables[0].Rows.Count;
                 mychart.Series[1].Points.Clear();
                 for (int i = 0; i < datagridview_row_count; i++)
                 {
                     if (gongyi_ds.Tables[0].Rows[i][4].ToString() == "温控")
                     {
                         int wendu = int.Parse(gongyi_ds.Tables[0].Rows[i][1].ToString());
                         mychart.Series[1].Points.AddXY(starttime, wendu);
                         int timespan = int.Parse(gongyi_ds.Tables[0].Rows[i][3].ToString());
                         starttime = starttime.AddMinutes(timespan);
                     }
                 }
             }
             catch
             {
             }
         }
     }
 }