zoukankan      html  css  js  c++  java
  • vs widows服务的开发

    最近再做一个视频管理系统,发现用户提交时实时转换视频非常慢。于是有了通过建立一个单独的服务。通过服务定时查询数据库,是否有需要转换的视频来解决问题。现把过程记录下来,已供参考

    1、新建widows 服务项目

      

    2、增加查询定时器在服务启动时

      protected override void OnStart(string[] args)
            {
            // 单位为毫秒
             System.Timers.Timer timer = new System.Timers.Timer(1000);
             
              timer.AutoReset = true;  
     
             timer.Enabled = true;
    
             timer.Elapsed += timer_Elapsed;  
     
              timer.Start();  
    
    
            }
    

    3、通过Timer的Elapsed事件处理定时任务

     void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {    // 加载外置配置文件
                      //得到当前服务的安装路径
                   string basepath = AppDomain.CurrentDomain.BaseDirectory;
                    XmlDocument xml = new XmlDocument();
              
                    xml.Load(basepath + "\videoconvertconfig.xml");
                    XmlNode rootNode = xml.SelectSingleNode("root");
                   
                   
                    string sqlConn = rootNode.Attributes["sqlConn"].Value;
                    //得到ffmpeg的路径
                    string ffmpegPath = rootNode.ChildNodes[0].Attributes["path"].Value;
    
                try
                {
                    using (SqlConnection conn = new SqlConnection(sqlConn))
                    {
                        conn.Open();
                        SqlDataAdapter data = new SqlDataAdapter("select videoaddress,id from viedoList where isconvert=0", conn);
                        DataSet ds = new DataSet();
                        data.Fill(ds);
                        DataTable VideoList = ds.Tables[0];
                        VideoConvert vc = new VideoConvert();
                        foreach (DataRow dr in VideoList.Rows)
                        {
                          // 视频转换
                            vc.convertVideo(basePath, ffmpegPath, dr[0].ToString()); 
                         //  转换成功 更新数据库
                            updateData(conn,dr[1].ToString());
                        }
                    }
    
                }
                catch (Exception err) { Common.WriteFile(basePath+"/log/log.txt", err.Message); }
            }
    

      

  • 相关阅读:
    python 连接sql server 解决中文乱码 及配置使用 web 服务使用
    Android调用.net的webservice服务器接收参数为空的情况
    好题推荐
    算法中一些trick和细节
    洛谷P2181 对角线
    新的开始
    文化课倒计时80天
    Electron-vue实现后台多进程(三. 自动化测试篇)
    工作感受月记202107月
    工作感受月记202106月
  • 原文地址:https://www.cnblogs.com/fogwang/p/4597873.html
Copyright © 2011-2022 走看看