2013-03-04 73 views
1

我有以下情形:Windows Mobile 6.1(.NET Compact Framwork 2.0)從服務器中消耗多個WCF服務的應用程序(同時運行於多個設備中)。要使用Windows Form應用程序,用戶必須登錄。如何:WCF服務檢查Windows窗體用戶是否仍然活動?

服務器消耗SAP系統中的數據。 WCF服務器必須根據記錄的用戶檢索來自SAP的一些信息。所以,我的Windows窗體應用程序必須(不時)顯示它們仍然運行到我的WCF服務器。

我該怎麼做才能完成這項任務?我在想創建一個後臺任務來更新WCF服務器的SQL服務器。 (如會話控制)。

+0

爲什麼不添加一個只是SillAlive(UserName)函數的WCF服務,並從設備的窗體應用程序中的後臺線程或計時器中調用該服務? – josef 2013-03-04 18:50:02

+0

WCF需要知道在特定時間內哪些設備處於活動狀態。由於某些設備最終可能會凍結或耗盡電池或無法使用(無法使用wi-fi範圍),因此我無法簡單地依賴於被召喚的服務商。這就是爲什麼我在控制使用SQL的狀態。 – Andrew 2013-03-04 19:54:01

回答

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; 
} 

請給我一個upvote,如果它有幫助。

標記爲答案如果它解決了您的問題。

+0

嗯,它不能直接解決我的問題,雖然它幫助解決了我的另一個問題。唯一的問題是我使用CF 2.0並且我不能更改爲3.5 ... – Andrew 2013-03-05 19:40:41

+1

您可以安裝CF 3.5。它可能已經在您的開發PC上。請參見[如何:在設備上安裝.NET Compact Framework](http://msdn.microsoft.com/zh-cn/library/bb788171(v = vs.90).aspx)。就我個人而言,我使用Windows資源管理器選項進行安裝,而不是將其與設備一起部署,因爲使用設備部署選項會使您的所有構建時間過長。 – jp2code 2013-03-05 22:49:56

+1

在安裝應用程序之前,您必須先安裝CF 3.5,然後'WinMobil'設備會告訴您需要重新啓動。重新啓動後,請安裝您的應用程序。十分簡單! :) – jp2code 2013-03-05 22:50:58

相關問題