關於windows 服務的實踐

來源:互聯網
上載者:User

標籤:

      最近工作上有定時更新資料庫中某產品數量的要求,主要是同步記錄顯示產品的使用方式。開始是在Global.asax檔案中通過寫計時器進行更新。但是資料庫中的資料還是沒有更新。而寫的txt日誌上有記錄。網上看了很多類型的情況。有人說是iis回收了。並且是只是第一個訪問的使用者才會對設定的定時器有效。於是,根據博友的建議嘗試寫個window服務。

以前也沒有弄過,找了下資料。依葫蘆畫瓢建立項目->選擇windows 服務,在產生的Service1.cs檔案中開啟右鍵查看代碼。在對應的方法上寫了計時器和資料操作。

聲明定時器:  System.Timers.Timer timer1;

OnStart方法:

           timer1 = new System.Timers.Timer();

            timer1.Interval = 600000;  //設定計時器事件間隔執行時間

            timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);

            timer1.Enabled = true;

 //執行SQL語句或其他動作
            try
            {
                int starttime = DateTime.Now.Hour;
                if (starttime ==20)
                {

                  DataClassesDataContext db = new DataClassesDataContext();
                    System.IO.StreamWriter sw = System.IO.File.AppendText("D:\\1.txt");
                    string w = "也許你也陽光,但是不夠有錢。" + "\r\n"; ;
                    sw.Write(w);
                    sw.Close();

  A_Se h = new A_Se();                  

   h.Location = "杜拜";                   

   h.Name = "chen";                    

   h.Num = 10;                    

   h.Code = "001";                    

   h.Username = "小名";                    

  h.Updatedate = DateTime.Now;

  db.A_Se.InsertOnSubmit(h);                    

  db.SubmitChanges();

                }          

           }  catch (Exception ex){  

                  Console.Write(ex.Message);            

           }

        }

接著是在視圖介面點擊添加安裝程式。會產生ProjectInstaller.cs。開啟會有serviceProcessInstaller1組件,點擊設定屬性的account的值為LocalSystem。serviceInstaller1組件設定屬性ServiceName的值.這就是你在工作管理員,服務裡面看到的那個名稱,StartType設定為Automatic自動。在右鍵代碼裡面添加如下代碼:

  public ProjectInstaller()        

{            

InitializeComponent();            

this.Committed += new InstallEventHandler(ProjectInstaller_.Committed);     

    }

        private void ProjectInstaller_.Committed(object sender, InstallEventArgs e)         {   

          //參數為服務的名字            

       System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController("ServiceName的值");            

       controller.Start();      

   }

在產生功能表列點擊產生(解決方案的名稱)。然後通過運行vs的命令提示 installutil window服務路徑進行安裝,安裝啟動後我們就可以在工作管理員的服務選項卡看到我們的那個服務了。卸載的命令:installutil /u window服務路徑.這裡安裝的時候一般要放到系統對應的架構目錄下,否則會安裝異常。我這裡用了linq to sql.查看系統日誌時提示sa使用者提示密碼不對。但我一直串連都是sa賬戶密碼應該沒有問題,後來我更改串連為本地(.)就可以更新資料了。

很多東西做過了不記下來很快就忘記了。所以決定以後都寫下自己經曆過的新知識點。好好實踐總結提高自己。

 

關於windows 服務的實踐

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.