اطلب من نظام التشغيل إعادة تحميل قيم مهلة الطاقة من السجل

StackOverflow https://stackoverflow.com/questions/3250929

سؤال

لدي بعض التعليمات البرمجية التي أستخدمها لتحديث مقدار الوقت الذي سينتظره الجهاز قبل النوم.

أقوم بتحديث السجل ثم أحاول إخبار نظام التشغيل بإعادة تحميل القيمة، لكنه لا يعمل.(جزء إعادة التحميل لا يعمل.يتم تحديث التسجيل بشكل جيد.)

سأقوم بنشر الكود الخاص بي على أمل أن يعرف شخص ما الخطأ الذي أفعله:

// Change the amount of seconds that the OS will wait before it goes to sleep.
public void ChangeBatteryTimeout(int timeoutInSeconds)
{
    // Attempt to open the key
    RegistryKey key = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Power\\Timeouts", true) ??
                      // If the return value is null, the key doesn't exist, so create it.
                      Registry.LocalMachine.CreateSubKey("SYSTEM\\CurrentControlSet\\Control\\Power\\Timeouts");

    if (key == null)
        throw new KeyNotFoundException("Could not find or make the key for timeout manangment.");

    // This value must be set for the timeout stuff to work.  
    //  See:http://msdn.microsoft.com/en-us/library/aa932196.aspx
    if (key.GetValue("ACUserIdle") == null)
        key.SetValue("ACUserIdle", 0, RegistryValueKind.DWord);

    // Set the Battery Suspend Timeout to be the passed in value.
    key.SetValue("BattSuspendTimeout", timeoutInSeconds, RegistryValueKind.DWord); 

    // Signal the OS to reload the value we just changed.
    DoAutoResetEvent();
}

// Tell to OS to reload the timeouts.
private static void DoAutoResetEvent()
{
    const string eventString = "PowerManager/ReloadActivityTimeouts";
    IntPtr newHandle = CreateEvent(IntPtr.Zero, false, false, eventString);
    EventModify(newHandle, (int)EventFlags.EVENT_SET);
    CloseHandle(newHandle); 
}


private enum EventFlags
{
    EVENT_PULSE = 1,EVENT_RESET = 2,EVENT_SET = 3
}

[DllImport("coredll.dll", SetLastError = true)]
private static extern IntPtr CreateEvent(IntPtr lpEventAttributes, 
    bool bManualReset, bool bInitialState, string lpName);

[DllImport("coredll")]
static extern bool EventModify(IntPtr hEvent, int func);

[DllImport("coredll.dll", SetLastError = true)]
private static extern bool CloseHandle(IntPtr hObject);

أنا أسمي هذا مع ChangeBatteryTimeout(10); ولكن، أثناء تغيير السجل، لا يدخل الجهاز في وضع السكون خلال 10 ثوانٍ.(يستغرق الأمر القيمة المحددة مسبقًا وهي 190 ثانية.)

سيكون موضع تقدير أي مساعدة.

هل كانت مفيدة؟

المحلول

ألقِ نظرة على الكود المصدري للوحة التحكم في المصدر العام لـ CE (لقد قمت بتنزيل Platform Builder الآن، أليس كذلك؟).IIRC عند إجراء التغيير فإنه يبث ملف WM_SETTINGCHANGE رسالة.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top