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);
}
}
}
当我开始写入服务 “手机未连接” 到事件日志。任何想法我做错了什么?或者至少如何查明错误...
谢谢。
嗯,仍然没有工作:(我将连接移到另一个方法,并在onStart中使用计时器调用它...同样的事情发生了我做了一个方法,写一个txt文件(只是为了看看服务工作正常),并从onStart调用它 - 它工作正常,但电话连接没有。我还能做些什么吗?是否有限制什么Windows服务可以做(端口连接等)? – Alex 2011-03-24 17:14:52
我不想想,Windows服务有这样的限制,但不知道为什么我告诉你要放一个计时器,因为onStart事件启动并完成,当你启动它自己的服务 – Anuraj 2011-03-25 02:45:58
任何其他想法如何使这项工作?? – Alex 2011-03-25 08:42:33