2011-03-24 57 views
0

我使用此示例http://www.codeproject.com/KB/cs/SMS.aspx來製作將通過手機發送短信的應用程序。當我做一個GUI贏取應用程序時,一切正常,但當我嘗試將其轉換爲Windows服務應用程序(無GUI)在後臺工作時,它告訴我沒有連接電話。將贏取應用程序轉換爲贏取服務

這裏都是很簡單,舉例:

GUI應用程序

using System; 
using System.Windows.Forms; 
using GsmComm.GsmCommunication; 
using GsmComm.PduConverter; 

namespace SMS.Forms 
{ 
    public partial class SendSMS : Form 
    { 
     SmsSubmitPdu pdu; 
     private int port; 
     private int baudrate; 
     private int timeout; 

     public SendSMS() 
     { 
      InitializeComponent(); 

      //phone connection 
      port = 3; 
      baudrate = 115200; 
      timeout = 300; 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      GsmCommMain comm = new GsmCommMain(port, baudrate, timeout); 

      try 
      { 
       comm.Open(); 

       //send sms 
       pdu = new SmsSubmitPdu("Test msg", "0xxxxxxxxx", ""); 
       comm.SendMessage(pdu); 

       comm.Close(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(this, "Connection error: " + ex.Message, "Connection setup", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
       return; 
      } 
      MessageBox.Show(this, "Successfully connected to the phone.", "Connection setup", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     } 
    } 
} 

WIN服務

using System; 
using System.Diagnostics; 
using System.ServiceProcess; 
using GsmComm.GsmCommunication; 
using GsmComm.PduConverter; 

namespace SMS 
{ 
    public partial class SendSMS : ServiceBase 
    { 

     SmsSubmitPdu pdu; 

     //logs 
     private string sSource; 
     private string sLog; 
     private string sEvent; 

     private int port; 
     private int baudrate; 
     private int timeout; 

     public SendSMS() 
     { 
      InitializeComponent(); 

      //event logs 
      sSource = "SendSMS"; 
      sLog = "SMS"; 
      if (!EventLog.SourceExists(sSource)) 
       EventLog.CreateEventSource(sSource, sLog); 


      //phone connection 
      port = 3; 
      baudrate = 115200; 
      timeout = 300; 
     } 

     protected override void OnStart(string[] args) 
     { 
      //logs 
      sEvent = "SMS service started"; 
      EventLog.WriteEntry(sSource, sEvent); 

      GsmCommMain comm = new GsmCommMain(port, baudrate, timeout); 

      try 
      { 
       comm.Open(); 
       while (!comm.IsConnected()) 
       { 
        sEvent = "Phone not connected"; 
        EventLog.WriteEntry(sSource, sEvent); 
        comm.Close(); 
        return; 
       } 

       //send sms 
       pdu = new SmsSubmitPdu("Test msg", "0xxxxxxxxx", ""); 
       comm.SendMessage(pdu); 

       comm.Close(); 
      } 
      catch (Exception ex) 
      { 
       sEvent = "Not done " + ex.Message; 
       EventLog.WriteEntry(sSource, sEvent); 
       return; 
      } 
      finally 
      { 
       comm.Close(); 
      } 
     } 

     protected override void OnStop() 
     { 
      //logs 
      sEvent = "SMS service stopped"; 
      EventLog.WriteEntry(sSource, sEvent); 
     } 
    } 
} 

當我開始寫入服務 「手機未連接」 到事件日誌。任何想法我做錯了什麼?或者至少如何查明錯誤...

謝謝。

回答

0

您不需要在onStart事件中編寫手機連接代碼,而需要創建計時器類的實例,並檢查手機是否按特定時間間隔連接。您可以在onstart事件中啓動計時器。

+0

嗯,仍然沒有工作:(我將連接移到另一個方法,並在onStart中使用計時器調用它...同樣的事情發生了我做了一個方法,寫一個txt文件(只是爲了看看服務工作正常),並從onStart調用它 - 它工作正常,但電話連接沒有。我還能做些什麼嗎?是否有限制什麼Wi​​ndows服務可以做(端口連接等)? – Alex 2011-03-24 17:14:52

+0

我不想想,Windows服務有這樣的限制,但不知道爲什麼我告訴你要放一個計時器,因爲onStart事件啓動並完成,當你啓動它自己的服務 – Anuraj 2011-03-25 02:45:58

+0

任何其他想法如何使這項工作?? – Alex 2011-03-25 08:42:33

相關問題