C# Global定时执行Global文件aTimer处理

本文详细介绍了如何在ASP.NET应用程序中使用异步线程池和定时器来执行后台任务,包括定义全局应用程序类,注册事件处理程序,并通过定时器触发任务执行,展示了异步任务管理的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    public class Global : System.Web.HttpApplication
    {
        private static event Action eventActions;
        /// <summary>
        ///  当前正在运行的异步线程池
        /// </summary>
        private static Dictionary<string, IAsyncResult> diyAsync = new Dictionary<string, IAsyncResult>();
        protected void Application_Start(object sender, EventArgs e)
        {
            eventActions += MyClass.Writer;//任务(可以处理多个方法)
            System.Timers.Timer aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 200;
            aTimer.Enabled = true;
        }
        /// <summary>
        /// 定时执行任务
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            foreach (Action action in eventActions.GetInvocationList())
            {
                try
                {
                    if (diyAsync.Any(p => p.Key == action.Method.Name) && !diyAsync[action.Method.Name].IsCompleted)
                        continue;
                    diyAsync[action.Method.Name] = action.BeginInvoke(null, null);
                }
                catch (Exception ex)
                {
                    //任务执行出错
                }
            }
        }
        protected void Session_Start(object sender, EventArgs e)
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            WH_WriteLog.WriteLog(name);
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
           string name=System.Reflection.MethodBase.GetCurrentMethod().Name;
           WH_WriteLog.WriteLog(name);
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            string name=System.Reflection.MethodBase.GetCurrentMethod().Name;
            WH_WriteLog.WriteLog(name);
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            WH_WriteLog.WriteLog(name);
            WH_WriteLog.WriteLog(e.ToString());
        }

        protected void Session_End(object sender, EventArgs e)
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            WH_WriteLog.WriteLog(name);
        }

        protected void Application_End(object sender, EventArgs e)
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            WH_WriteLog.WriteLog(name);
        }
    }

    public class MyClass
    {
        public static void Writer()
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            WH_WriteLog.WriteLog(name);
        }
    }

 

转载于:https://www.cnblogs.com/-hao/p/9944813.html

private void ConnectPlatform() //打开控制卡与配置 { int netid, imcid, i; string err; netid = NetworkCard.SelectedIndex; imcid = ControlCardID.SelectedIndex; if (Global.isOpen())//设备已打开则关闭 { IMC_Pkg.PKG_IMC_Close(Global.g_handle); Global.g_handle = IntPtr.Zero; OpenControlCard.Text = "打开控制卡"; timer1.Enabled = false; } else { //有3种方法可连接控制卡,根据需要选择其中一个方法 #if true //方法1, Global.g_handle = IMC_Pkg.PKG_IMC_Open(netid, imcid); #elif false //方法2,使用方法1无法打开通过无线网卡连接的控制卡时,可使用方法2打开 Global.g_handle = IMC_Pkg.PKG_IMC_OpenX(netid, imcid, 40, 1); #else //方法3,使用密码连接控制卡 byte[] password = new byte[32]; string pw = "123456"; //此密码由iMCAT-6xxE软件或者PKG_IMC_SetPassword函数得到 password = System.Text.Encoding.Default.GetBytes(pw); Global.g_handle = IMC_Pkg.PKG_IMC_OpenUsePassword(netid, imcid, ref password[0], 6); #endif if (Global.isOpen()) { OpenControlCard.Text = ("关闭控制卡"); timer1.Enabled = true; //启动定时器,定时读取位置信息 Global.GetIMCInfo(); if (Global.g_naxis > 0) { ListViewItem lvitem; AxisMonitor.Items.Clear(); for (i = 0; i < Global.g_naxis; i++) { lvitem = AxisMonitor.Items.Add("轴" + i.ToString()); lvitem.SubItems.Add(("0")); lvitem.SubItems.Add(("0")); lvitem.SubItems.Add(("0x0000")); m_error_t[i] = 0; //存储第 i 个轴的错误状态或代码 m_curpos_t[i] = 0; //存储第 i 个轴的指令位置 m_encp_t[i] = 0; //存储第 i 个轴的编码器位置 } lvitem = AxisMonitor.Items.Add("encs"); lvitem.SubItems.Add(("0")); lvitem.SubItems.Add(("Ecat Error:")); lvitem.SubItems.Add(("0x00000000")); m_encs_t = 0; m_EcatErr_t = 0; } else { err = Global.GetFunErrStr(); MessageBox.Show(err); } } else { MessageBox.Show(("无法打开控制卡!\r\n\r\n请检查网卡和控制卡ID是否选择正确!")); } } } 我这个代码是在线程A中使用的,而NetworkCard、ControlCardID、timer1、AxisMonitor等控件是在UI线程的,我要如何修改代码
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值