發起GPRS撥號請求

freshairpeng發表於2009-03-04

using System;
using System.Collections.Generic;
using System.Text;
using OpenNETCF.Net;
using System.Windows.Forms;
using Microsoft.Win32;

namespace AnotherTodayScreenItem.Utl
{
    ///


    /// 發起GPRS撥號請求
    ///

    class ConnectNet
    {
        private OpenNETCF.Net.ConnectionManager ConnMgr = new OpenNETCF.Net.ConnectionManager();
        private DestinationInfoCollection cenum;
        private bool is_connected;
        private static volatile ConnectNet m_instance = null;

        public ConnectNet()
        {
        }

        public static ConnectNet singleton
        {
            get
            {
                if (m_instance == null)
                {
                    lock (typeof(ConnectNet))
                    {
                        if (m_instance == null)
                        {
                            m_instance = new ConnectNet();
                        }
                    }
                }

                return m_instance;
            }
        }


        public void ConnGPRS(bool connect)
        {
          
            long startTime;
            cenum = ConnMgr.EnumDestinations();
            List connectionDetail = ConnectionManager.ConnectionDetailItems;
            is_connected = false;
            try
            {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                foreach (ConnectionDetail em in connectionDetail)
                  {
                    if (em.Description == "linkgprs")
                    {
                        if (connect)
                        {
                            RegMobile regManager = new RegMobile();
                            regManager.changeValue(RegMobile.HKEY.HKEY_LOCAL_MACHINE, em.Description);
                            ConnMgr.Connect(em.DestinatonNetwork, true, ConnectionMode.Asynchronous);
                            //mmmmmmmmmmmmmmmmmmmmmmmmConnMgr.Connect(em.DestinatonNetwork, true, ConnectionMode.Asynchronous);
                            startTime = DateTime.Now.Ticks;
                            while ((!is_connected) && (DateTime.Now.Ticks - startTime < 20 * TimeSpan.TicksPerSecond)) ;
                            break;
                        }
                        else
                        {
                            ConnMgr.RequestDisconnect();
                        }
                    }
                }

            }
            catch (SystemException err)
            {
                MessageBox.Show(" err 700 " + err.Message);
            }
        }

        public void close_connect()
        {
            ConnMgr.RequestDisconnect();
        }
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/14766526/viewspace-561396/,如需轉載,請註明出處,否則將追究法律責任。

相關文章