4

我开发了一个 asp.net 应用程序,根据浏览器中的 URL 从 gsm 调制解调器向目标发送短信我使用了由 codeproject http://www.codeproject.com/articles/20420/how-to-send-and开发的库-receive-sms-using-gsm-modem

但是当我同时请求两个浏览器时遇到问题,我想让我的代码检测到调制解调器当时被另一个进程使用这是我的代码:

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");
        }

    }
}
4

1 回答 1

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
  }            
}

我同意 Dai 的全局互斥锁理念作为一种改进,请参阅What is a good pattern for using a Global Mutex in C#?

于 2012-09-24T14:09:24.097 回答