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

时间:2013-03-04 17:27:49

标签: c# winforms session windows-mobile

我有以下场景:Windows Mobile 6.1(.NET Compact Framwork 2.0)表单应用程序(同时在多个设备中运行),它从服务器消耗多个WCF服务。要使用Windows窗体应用程序,用户必须登录。

服务器使用SAP系统中的数据。 WCF服务器必须根据已记录的用户从SAP检索某些信息。因此,我的Windows窗体应用程序必须(不时)显示它们仍在运行到我的WCF服务器。

我可以做些什么来完成这项任务?我在考虑创建一个更新WCF服务器的SQL服务器的后台任务。 (比如会话控制)。

1 个答案:

答案 0 :(得分:1)

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

与手机一样,如果移动设备长时间不使用,操作系统将显示电池处于同一级别,直到实际使用完为止。所以,它不太可靠。

但是,您可以侦听事件(like 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;
}