如何:WCF服务检查Windows窗体用户是否仍处于活动状态

本文关键字:是否 活动状态 用户 窗体 WCF 服务 检查 Windows 如何 | 更新日期: 2023-09-27 18:02:24

我有以下场景:Windows Mobile 6.1 (. NET Compact framework 2.0)表单应用程序(同时在多个设备上运行),它使用来自服务器的多个WCF服务。要使用windows Form应用程序,用户必须登录。

服务器使用来自SAP系统的数据。WCF服务器必须根据记录的用户从SAP检索一些信息。所以,我的Windows窗体应用程序必须(不时)显示他们仍然运行到我的WCF服务器。

我该怎么做才能完成这个任务?我在考虑创建一个后台任务来更新WCF服务器的SQL服务器。

如何:WCF服务检查Windows窗体用户是否仍处于活动状态

您可以侦听应用程序中的Power State更改。文档显示它只适用于Windows Mobile 6.5,但我在运行. net CF 3.5的Windows Mobile 5.0应用程序中使用它。

和手机一样,如果移动设备长时间不使用,在实际使用之前,操作系统会显示电池处于相同的电量。所以,它不是很可靠。

然而,你可以监听事件(如POWER_STATE_CRITICAL等),并让你的软件做出相应的改变,与你的WCF服务器进行交互。

下面是我使用的类似的编辑版本。

这不会100%解决你的问题,但它应该给你一个如何做你需要的想法。

using Microsoft.WindowsMobile.Status;
BatteryLevel _batteryLevel;
BatteryState _batteryState;
void Mobile5_Load(object sender, EventArgs e) {
  _batteryLevel = (BatteryLevel)SystemState.GetValue(SystemProperty.PowerBatteryStrength);
  _batteryState = (BatteryState)SystemState.GetValue(SystemProperty.PowerBatteryState);
  if (!BatteryCritical(false)) {
    // Continue
  }
}
/// <summary>
/// Sets the Battery Level and Battery State for the Mobile Device
/// <para><value>showDialog=True show the dialog box</value></para>
/// <para><value>Returns True <b>if</b> the battery is in a critical state</value></para>
/// </summary>
/// <param name="showDialog">Do you want a dialog box to be displayed or not?</param>
/// <returns>false if the Battery is NOT in the critical state</returns>
bool BatteryCritical(bool showDialog) {
  _batteryAlert = false;
  bool bad = false; // everything starts out ok. We are actually running, after all.
  _batteryLevel = (BatteryLevel)SystemState.GetValue(SystemProperty.PowerBatteryStrength);
  _batteryState = (BatteryState)SystemState.GetValue(SystemProperty.PowerBatteryState);
  bool present = ((_batteryState & BatteryState.NotPresent) != BatteryState.NotPresent);
  bool charging = ((_batteryState & BatteryState.Charging) == BatteryState.Charging);
  bool critical = ((_batteryState & BatteryState.Critical) == BatteryState.Critical);
  bool lowbatry = ((_batteryState & BatteryState.Low) == BatteryState.Low);
  Color c;
  if (present) {
    if (charging) {
      c = Color.Cyan;
    } else {
      if (critical) {
        c = Color.Orange;
        _batteryAlert = true;
      } else if (lowbatry) {
        c = Color.Yellow;
        _batteryAlert = true;
      } else {
        c = Color.White;
      }
    }
  } else {
    c = Color.Silver;
  }
  StatusPanel.BackColor = c;
  switch (_batteryLevel) {
    case BatteryLevel.VeryHigh: // (81-100%)
      BatteryPanel.BackColor = Color.Cyan;
      break;
    case BatteryLevel.High: // (61-80%)
      BatteryPanel.BackColor = Color.Lime;
      break;
    case BatteryLevel.Medium: // 41-60%)
      BatteryPanel.BackColor = Color.Yellow;
      break;
    case BatteryLevel.Low: // (21-40%)
      BatteryPanel.BackColor = Color.Orange;
      //WirelessUpdate(RadioState.Off, false);
      break;
    case BatteryLevel.VeryLow: // (0-20%)
      BatteryPanel.BackColor = Color.Red;
      //WirelessUpdate(RadioState.Off, false);
      bad = (!charging && present);
      break;
  }
  if (showDialog) {
    string msg = string.Format("Level is {0}'r'nState is {1}", _batteryLevel, _batteryState);
    if (_batteryLevel == BatteryLevel.Low) {
      msg += "'r'n'r'nThe wireless radio will be inactive until it has been charged.";
    } else if (bad == true) {
      msg += "'r'n'r'nThis Application will now close to preserve the device. Please return it to a charging base immediately.";
    }
    MessageBox.Show(msg, "Battery Meter", MessageBoxButtons.OKCancel, MessageBoxIcon.None, 0);
  }
  if (!bad) {
    StatusPanel.Refresh();
    // You could signal your app here
  } else {
    // Tell your app this device needs to turn off now.
  }
  return bad;
}