2012-09-24 87 views
4

我devloped一個asp.net應用程序從GSM調制解調器發送短信到目標基礎上,網址從我使用的庫瀏覽器,通過CodeProject上http://www.codeproject.com/articles/20420/how-to-send-and-receive-sms-using-gsm-modem發送短信在asp.net通過GSM調制解調器

開發,但我得到的問題當我請求在同一時間形成了兩個瀏覽器,我想使我的代碼檢測調制解調器是由另一個進程使用的時候 這裏是我的代碼:

DeviceConnection deviceConnection = new DeviceConnection(); 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      try 
      { 
       if (Request.QueryString["destination"] != null && Request.QueryString["text"] != null) 
       { 
        deviceConnection.setBaudRate(9600); 
        deviceConnection.setPort(12); 
        deviceConnection.setTimeout(200);    
        SendSms sendSms = new SendSms(deviceConnection); 
        if (deviceConnection.getConnectionStatus()) 
        { 

         sendSms.strReciverNo = Request.QueryString["destination"]; 
         sendSms.strTextMessage = Request.QueryString["text"]; 

         if (sendSms.sendSms()) 
         { 
          Response.Write("Mesage successfuly sent to " + Request.QueryString["destination"]); 
         } 
         else 
         { 
          Response.Write("Message was not sent"); 
         } 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine("Index "+ex.StackTrace); 
      } 
     } 

這是SendSms類:

class SendSms 
    { 
     DeviceConnection deviceConnection; 
     public SendSms(DeviceConnection deviceConnection) 
     { 
      this.deviceConnection = deviceConnection; 
     } 
     private string reciverNo; 
     private string textMessage; 
     private delegate void SetTextCallback(string text); 
     public string strReciverNo 
     { 
      set 
      { 
       this.reciverNo = value; 
      } 
      get 
      { 
       return this.reciverNo; 
      } 
     } 
     public string strTextMessage 
     { 
      set 
      { 
       this.textMessage = value; 
      } 
      get 
      { 
       return this.textMessage; 
      } 
     } 

     public bool sendSms() 
     { 
      try 
      { 
       CommSetting.Comm_Port = deviceConnection.getPort();//GsmCommMain.DefaultPortNumber; 
       CommSetting.Comm_BaudRate = deviceConnection.getBaudRate(); 
       CommSetting.Comm_TimeOut = deviceConnection.getTimeout();//GsmCommMain.DefaultTimeout; 

       CommSetting.comm = new GsmCommMain(deviceConnection.getPort() 
        , deviceConnection.getBaudRate(), deviceConnection.getTimeout()); 
       // CommSetting.comm.PhoneConnected += new EventHandler(comm_PhoneConnected); 
       if (!CommSetting.comm.IsOpen()) 
       { 
        CommSetting.comm.Open(); 
       } 
       SmsSubmitPdu smsSubmitPdu = new SmsSubmitPdu(strTextMessage, strReciverNo, ""); 
       smsSubmitPdu.RequestStatusReport = true; 
       CommSetting.comm.SendMessage(smsSubmitPdu); 
       CommSetting.comm.Close(); 
       return true; 
      } 
      catch (Exception exception) 
      { 
       Console.WriteLine("sendSms " + exception.StackTrace); 
       CommSetting.comm.Close(); 
       return false; 
      } 
     } 
     public void recive(object sender, EventArgs e) 
     { 
      Console.WriteLine("Message received successfuly"); 
     } 

    } 
} 

回答

0

我不確定ASP的工作原理,但是如果每個進程共享相同的內存空間,則可以使用靜態鎖來確保單一訪問。無論如何值得一試。

private static readonly object CommLock = new object(); 

public bool sendSms() 
{ 
    lock(CommLock) //second request should wait here until first request finishes 
    { 
    //all the same comm code from sendSms as before 
    }    
} 

我戴的全球互斥的想法作爲改進同意,見What is a good pattern for using a Global Mutex in C#?

+1

所有的請求都通過相同的方法在相同的AppDomain中處理(除在非常特殊的情況下),讓您的解決方案將工作 - 但是要絕對確定我會使用OS級別的互斥鎖或鎖定文件。 – Dai

相關問題