线程池回调

来源:互联网 发布:马伯庸 知乎 长安 编辑:程序博客网 时间:2024/04/28 15:22

“线程池”是可以用来在后台执行多个任务的线程集合;

所谓线程池回调技术是指线程池中指定的线程定期执行指定的函数.

以下是用改技术获取gps数据的例子:

using System.IO.Ports;
using System.Threading;
using com.ay.pda.device.gps;
using System.Runtime.InteropServices;

 

namespace com.ay.pda
{
    public partial class GPSForm : Form
    {

 

         public System.Threading.Timer _gpggaTimer = null;
         delegate void UpdateFormDelegate();

         GPS gps = null;

        public GPSForm(com.ay.pda.config config)
        {
            InitializeComponent();
            gps = new GPS(gpsfilepath);
            Cursor.Current = Cursors.Default;
        }

         public void GPSForm_Load(object sender, EventArgs e)
        {
            object threadName = "GPGGA";
            TimerCallback tmrClbck = new TimerCallback(MethodInvok);//声明线程回调委托

            _gpggaTimer = new System.Threading.Timer(tmrClbck, threadName, 3000, -1);//实例化委托
        }

 

private void MethodInvok(object threadName)
        {
             try
            {
                object gpsResult = new object();
                if (_gpggaTimer != null)
                    gpsResult = this.Invoke(new com.ay.pda.device.gps.GPS.gpsDelegate(gps.initPosition), new object[] { _GPSTimes });
                  if (_gpggaTimer == null)
                {
                    return;//该资源已经被释放了
                }

                if (Convert.ToString(gpsResult) == "sucess")
                {
                   
                    this.Invoke(new UpdateFormDelegate(UpdateForm));

                    if (_gpsState)
                    {
                        _gpsState = false;
                        _gpggaTimer.Change(3000, -1); //如果现在已经获的了GPS数据则3秒更新一次
                    }
                    else
                        _gpggaTimer.Change(4000, -1);
                 }
                else
                {
                    if (_gpsState)
                    {
                        _gpsState = false;
                        _gpggaTimer.Change(4000, -1); //如果现在已经获的了GPS数据则3秒更新一次
                    }
                    else
                        _gpggaTimer.Change(5000, -1);
                }
            }
            catch
            {
                return;
            }
        }

        private void UpdateForm()
        {
            if (gps.getN() > 0)
            {
                textBoxYlat.Text = Convert.ToString(gps.getN());
                main._PosY = Convert.ToString(gps.getN());
            }
            if (gps.getE() > 0)
            {
                textBoxXlong.Text = Convert.ToString(gps.getE());
                main._PosX = Convert.ToString(gps.getE());
            }
           
            lab_time.Text = "时间:" + gps.getTime();
            lab_height.Text = "海拔:" + gps.getHeight()+"米";
            lab_speed.Text = "速度:" + Convert.ToString(gps.getSpeed())+"/公里";
            lab_satellite.Text = "卫星:"+gps.getSatelliteCount()+"颗";
            if (_gpsVoice )
            {
                PlaySound(config.getVoicePath("获取GPS数据成功"), IntPtr.Zero, 0x0001 | 0x00020000);
                _gpsVoice = false;
               
            }
        }

原创粉丝点击