restarting services using SMSlib issues

977 views Asked by At

I am currently facing a problem related to SMSlib for .NET library (you can download it at http://www.smslib.org/) btw I used SendMessage example (you can find it in ...\Examples\SendMessage) found in the library, and tried to compile and run it using VS2012...it worked fine , but it never succeeded when I tried to restart the services again after adding a little bit code to restart the services into it...

Do you know how to make the services restart properly?

btw part of the code is as follows :

    try
        {
            Console.WriteLine("Example: Read messages from a serial gsm modem.");
            Console.WriteLine(Library.getLibraryDescription());
            Console.WriteLine("Version: " + Library.getLibraryVersion());

            // Start the COM listening thread.
            new Thread(new ThreadStart(com1.Run)).Start();

            // Lets set some callbacks.
            srv.setInboundMessageNotification(new InboundNotification());
            srv.setCallNotification(new CallNotification());
            srv.setGatewayStatusNotification(new GatewayStatusNotification());

            // Create the Gateway representing the serial GSM modem.
            // Due to the Comm2IP bridge, in SMSLib for .NET all modems are considered IP modems.
            IPModemGateway gateway = new IPModemGateway("modem.com14", "127.0.0.1", 12000, "Huawei","E173");
            gateway.setIpProtocol(ModemGateway.IPProtocols.BINARY);

            // Set the modem protocol to PDU (alternative is TEXT). PDU is the default, anyway...
            gateway.setProtocol(AGateway.Protocols.PDU);

            // Do we want the Gateway to be used for Inbound messages?
            gateway.setInbound(true);

            // Do we want the Gateway to be used for Outbound messages?
            gateway.setOutbound(true);



            // Add the Gateway to the Service object.
            srv.addGateway(gateway);

            // Similarly, you may define as many Gateway objects, representing
            // various GSM modems, add them in the Service object and control all of them.

            // Start! (i.e. connect to all defined Gateways)
            srv.startService();

            // Printout some general information about the modem.
            Console.WriteLine();
            Console.WriteLine("Modem Information:");
            Console.WriteLine("  Manufacturer: " + gateway.getManufacturer());
            Console.WriteLine("  Model: " + gateway.getModel());
            Console.WriteLine("  Serial No: " + gateway.getSerialNo());
            Console.WriteLine("  SIM IMSI: " + gateway.getImsi());
            Console.WriteLine("  Signal Level: " + gateway.getSignalLevel() + "dBm");
            Console.WriteLine("  Battery Level: " + gateway.getBatteryLevel() + "%");
            Console.WriteLine();

            // Send one message.
            // Remember to change the recipient!
            OutboundMessage msg = new OutboundMessage("0952998989", "Hello from SMSLib for .NET");
            srv.sendMessage(msg);
            Console.WriteLine(msg);



            Console.WriteLine("Press <ENTER> to terminate...");
            Console.In.ReadLine();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            Console.WriteLine(e.StackTrace);
        }
        finally
        {
            com1.Stop();
            srv.stopService();

            // Start! (i.e. connect to all defined Gateways)
            srv.startService();
            new Thread(new ThreadStart(com1.Run)).Start();
        }
1

There are 1 answers

1
Thanasis Delenikas On

You should not call com1.Stop(); until you're completely done with starting/stopping the service. Just leave the listener active.

E.g.

finally
{
  srv.stopService();
  srv.startService();
  srv.stopService();
  srv.startService();
  srv.stopService();
  srv.startService();
  srv.stopService();
  srv.startService();
  srv.stopService();
  // ... and finally ...
  com1.Stop();
}