如何检查PPC与WebService之间的连接?

发布于 2024-10-19 07:46:57 字数 50 浏览 8 评论 0原文

如何检查 PPC 与服务器上的 Web 服务之间的连接?我在 FW3.5 C# 上工作

How do I check the connection between PPC and a web service on the server? I work on FW3.5 C#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡风轻 2024-10-26 07:46:57

我记不清这是否在 .Net CF 中可用:

System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged

另一种选择是尝试使用 Web 服务并处理异常。我通常遵循这种模式,有什么原因它不适用于您的情况吗?

编辑:

连接监视器类:

    public static class ConnectionMonitor
    {
        public static event EventHandler IsConnectedChanged;

        private static SystemState _connectionState = null,
            //_cellState = null,
            _gprsState = null;
        private static int _connectionCount = 0;
            //_cellCount = 0;
        private static bool _gprs = false;
        private static bool _isConnected = true;
        private static string _phoneCarrier = SystemState.PhoneOperatorName;
        private const int POWER_FLAGS = 0x00000001; // default
        private const string AXIM_WIFI_ADAPTER = "{98C5250D-C29A-4985-AE5F-AFE5367E5006}\\TIACXWLN1";

        public static bool IsPhone
        {
            get { return !string.IsNullOrEmpty(_phoneCarrier); }
        }

        public static bool IsGPRSConnected
        {
            get { return _gprs; }
        }

        public static bool IsConnected
        {
            get { return _isConnected; }
            set
            {
                if (_isConnected != value)
                {
                    _isConnected = value;
                    if (IsConnectedChanged != null)
                    {
                        IsConnectedChanged(null, EventArgs.Empty);
                    }

                    if (!_isConnected && !IsPhone)
                    {
                        WifiOn();  
                    }
                }
            }
        }

        private static void WifiOn()
        {
            Win32.CEDEVICE_POWER_STATE state = new Win32.CEDEVICE_POWER_STATE();
            if (Win32.GetDevicePower(AXIM_WIFI_ADAPTER, POWER_FLAGS, ref state) == 0)
            {
                if (state != Win32.CEDEVICE_POWER_STATE.D0)
                {
                    Win32.DevicePowerNotify(AXIM_WIFI_ADAPTER, Win32.CEDEVICE_POWER_STATE.D4, POWER_FLAGS);
                    Win32.SetDevicePower(AXIM_WIFI_ADAPTER, POWER_FLAGS, Win32.CEDEVICE_POWER_STATE.D0);
                }
            }
        }

        public static void Init()
        {
            if (_connectionState == null)
            {
                _connectionState = new SystemState(SystemProperty.ConnectionsCount);
                _connectionCount = SystemState.ConnectionsCount;
                _connectionState.Changed += new ChangeEventHandler(_state_Changed);
            }
            //if (_cellState == null)
            //{
            //    _cellState = new SystemState(SystemProperty.ConnectionsCellularCount);
            //    _cellCount = SystemState.ConnectionsCellularCount;
            //    _cellState.Changed += new ChangeEventHandler(_state_Changed);
            //}
            if (_gprsState == null)
            {
                _gprsState = new SystemState(SystemProperty.PhoneGprsCoverage);
                _gprs = SystemState.PhoneGprsCoverage;
                _gprsState.Changed += new ChangeEventHandler(_state_Changed);
            }
            IsConnected =  _connectionCount > 0 || _gprs;
        }

        private static void _state_Changed(object sender, ChangeEventArgs args)
        {
            //_cellCount = SystemState.ConnectionsCellularCount;
            _connectionCount = SystemState.ConnectionsCount;
            _gprs = SystemState.PhoneGprsCoverage;
            IsConnected = _connectionCount > 0 || _gprs;
        }

        public static void Dispose()
        {
            if (_connectionState != null)
            {
                _connectionState.Changed -= new ChangeEventHandler(_state_Changed);
                _connectionState.Dispose();
                _connectionState = null;
            }
            //if (_cellState != null)
            //{
            //    _cellState.Changed -= new ChangeEventHandler(_state_Changed);
            //    _cellState.Dispose();
            //    _cellState = null;
            //}
            if (_gprsState != null)
            {
                _gprsState.Changed -= new ChangeEventHandler(_state_Changed);
                _gprsState.Dispose();
                _gprsState = null;
            }
        }
}

我的 Win32 静态类中有很多调用(与您的需求无关)。以下是我认为您需要的所有内容:

        public enum CEDEVICE_POWER_STATE : int
        {
            PwrDeviceUnspecified = -1,
            D0 = 0, // Full On: full power, full functionality
            D1 = 1, // Low Power On: fully functional at low power/performance
            D2 = 2, // Standby: partially powered with automatic wake
            D3 = 3, // Sleep: partially powered with device initiated wake
            D4 = 4, // Off: unpowered
            PwrDeviceMaximum = 5
        }

        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int DevicePowerNotify(string name, CEDEVICE_POWER_STATE state, int flags);

        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int SetDevicePower(string name, int flags, CEDEVICE_POWER_STATE state);

        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int GetDevicePower(string name, int flags, ref CEDEVICE_POWER_STATE state);

如果我错过了任何内容,请在 pinvoke.net 上查找。

I can't remember offhand if this is available in .Net CF or not:

System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged

Another option is to just attempt to use the web service, and handle exceptions. I usually follow this pattern, is there a reason it doesn't work for your case?

Edit:

Connection Monitor class:

    public static class ConnectionMonitor
    {
        public static event EventHandler IsConnectedChanged;

        private static SystemState _connectionState = null,
            //_cellState = null,
            _gprsState = null;
        private static int _connectionCount = 0;
            //_cellCount = 0;
        private static bool _gprs = false;
        private static bool _isConnected = true;
        private static string _phoneCarrier = SystemState.PhoneOperatorName;
        private const int POWER_FLAGS = 0x00000001; // default
        private const string AXIM_WIFI_ADAPTER = "{98C5250D-C29A-4985-AE5F-AFE5367E5006}\\TIACXWLN1";

        public static bool IsPhone
        {
            get { return !string.IsNullOrEmpty(_phoneCarrier); }
        }

        public static bool IsGPRSConnected
        {
            get { return _gprs; }
        }

        public static bool IsConnected
        {
            get { return _isConnected; }
            set
            {
                if (_isConnected != value)
                {
                    _isConnected = value;
                    if (IsConnectedChanged != null)
                    {
                        IsConnectedChanged(null, EventArgs.Empty);
                    }

                    if (!_isConnected && !IsPhone)
                    {
                        WifiOn();  
                    }
                }
            }
        }

        private static void WifiOn()
        {
            Win32.CEDEVICE_POWER_STATE state = new Win32.CEDEVICE_POWER_STATE();
            if (Win32.GetDevicePower(AXIM_WIFI_ADAPTER, POWER_FLAGS, ref state) == 0)
            {
                if (state != Win32.CEDEVICE_POWER_STATE.D0)
                {
                    Win32.DevicePowerNotify(AXIM_WIFI_ADAPTER, Win32.CEDEVICE_POWER_STATE.D4, POWER_FLAGS);
                    Win32.SetDevicePower(AXIM_WIFI_ADAPTER, POWER_FLAGS, Win32.CEDEVICE_POWER_STATE.D0);
                }
            }
        }

        public static void Init()
        {
            if (_connectionState == null)
            {
                _connectionState = new SystemState(SystemProperty.ConnectionsCount);
                _connectionCount = SystemState.ConnectionsCount;
                _connectionState.Changed += new ChangeEventHandler(_state_Changed);
            }
            //if (_cellState == null)
            //{
            //    _cellState = new SystemState(SystemProperty.ConnectionsCellularCount);
            //    _cellCount = SystemState.ConnectionsCellularCount;
            //    _cellState.Changed += new ChangeEventHandler(_state_Changed);
            //}
            if (_gprsState == null)
            {
                _gprsState = new SystemState(SystemProperty.PhoneGprsCoverage);
                _gprs = SystemState.PhoneGprsCoverage;
                _gprsState.Changed += new ChangeEventHandler(_state_Changed);
            }
            IsConnected =  _connectionCount > 0 || _gprs;
        }

        private static void _state_Changed(object sender, ChangeEventArgs args)
        {
            //_cellCount = SystemState.ConnectionsCellularCount;
            _connectionCount = SystemState.ConnectionsCount;
            _gprs = SystemState.PhoneGprsCoverage;
            IsConnected = _connectionCount > 0 || _gprs;
        }

        public static void Dispose()
        {
            if (_connectionState != null)
            {
                _connectionState.Changed -= new ChangeEventHandler(_state_Changed);
                _connectionState.Dispose();
                _connectionState = null;
            }
            //if (_cellState != null)
            //{
            //    _cellState.Changed -= new ChangeEventHandler(_state_Changed);
            //    _cellState.Dispose();
            //    _cellState = null;
            //}
            if (_gprsState != null)
            {
                _gprsState.Changed -= new ChangeEventHandler(_state_Changed);
                _gprsState.Dispose();
                _gprsState = null;
            }
        }
}

My Win32 static class had a lot of calls (unrelated to your needs) in it. Here are all the ones I think you need:

        public enum CEDEVICE_POWER_STATE : int
        {
            PwrDeviceUnspecified = -1,
            D0 = 0, // Full On: full power, full functionality
            D1 = 1, // Low Power On: fully functional at low power/performance
            D2 = 2, // Standby: partially powered with automatic wake
            D3 = 3, // Sleep: partially powered with device initiated wake
            D4 = 4, // Off: unpowered
            PwrDeviceMaximum = 5
        }

        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int DevicePowerNotify(string name, CEDEVICE_POWER_STATE state, int flags);

        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int SetDevicePower(string name, int flags, CEDEVICE_POWER_STATE state);

        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int GetDevicePower(string name, int flags, ref CEDEVICE_POWER_STATE state);

If I missed any, look them up on pinvoke.net.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文