首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将win应用转换为win服务

将win应用转换为win服务
EN

Stack Overflow用户
提问于 2011-03-24 20:19:39
回答 1查看 867关注 0票数 0

我正在使用这个示例http://www.codeproject.com/KB/cs/SMS.aspx来制作一个应用程序,它将通过移动电话发送短信。当我创建一个GUI win应用程序时,一切运行正常,但当我尝试将其转换为windows服务应用程序(无GUI)以在后台工作时,它告诉我没有连接电话。

下面是两个非常简单的例子:

GUI应用程序

代码语言:javascript
复制
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);
        }
    }
}

赢得服务

代码语言:javascript
复制
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);
        }
    }
}

当我启动该服务时,它会在事件日志中写入"Phone not connected“。你知道我做错了什么吗?或者至少如何定位错误..。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2011-03-24 20:23:20

您不需要在onStart事件中编写电话连接代码,而是需要创建timer类的实例,并检查电话是否按特定时间间隔连接。您可以在onstart事件中启动计时器。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5418978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档