ตัวจับเวลาเพื่อเริ่มเหตุการณ์ WPF

ฉันมีโปรเจ็กต์ที่นี่และได้ตั้งค่าไว้เป็นค่าเริ่มต้นว่าการกระทำจะเกิดขึ้นโดยเหตุการณ์ MouseEnter ฉันหมายถึงการเปิดหน้าต่าง ปิด กลับ อะไรก็ตาม เกิดขึ้นโดยเหตุการณ์ MouseEnter เท่านั้น

ฉันถูกขอให้ทำให้เหตุการณ์เริ่มทำงานหลังจากผ่านไป 3 วินาทีเท่านั้น นั่นหมายความว่าผู้ใช้จะวางเมาส์บนตัวควบคุมและหลังจากผ่านไป 3 วินาทีเท่านั้น เหตุการณ์จะต้องเกิดขึ้นกับตัวควบคุมทั้งหมดในหน้าต่าง

ดังนั้นฉันจึงคิดถึงตัวจับเวลาทั่วโลกหรืออะไรที่คล้ายกัน ซึ่งจะคืนค่าเท็จจนกว่าตัวจับเวลาจะถึง 3... ฉันคิดว่านั่นคือวิธี...

เอาน่า มีใครรู้บ้างไหมว่าฉันจะทำสิ่งนั้นได้อย่างไร?

ขอบคุณ!!


person Armando Freire    schedule 09.09.2012    source แหล่งที่มา
comment
คุณนอนไม่หลับสัก 3 วินาทีในกิจกรรม MouseEnter เหรอ?   -  person paparazzo    schedule 09.09.2012
comment
การหลับเป็นเวลา 3 วินาทีในเหตุการณ์ MouseEnter จะล็อค UI เป็นเวลา 3 วินาที   -  person Wonko the Sane    schedule 01.11.2012


คำตอบ (2)


คุณสามารถกำหนดคลาสที่จะเปิดเผยเมธอด DelayedExecute ที่ได้รับการดำเนินการเพื่อดำเนินการและสร้างตัวจับเวลาตามที่จำเป็นสำหรับการดำเนินการล่าช้า มันจะมีลักษณะเช่นนี้:

public static class DelayedExecutionService
{
    // We keep a static list of timers because if we only declare the timers
    // in the scope of the method, they might be garbage collected prematurely.
    private static IList<DispatcherTimer> timers = new List<DispatcherTimer>();

    public static void DelayedExecute(Action action, int delay = 3)
    {
        var dispatcherTimer = new System.Windows.Threading.DispatcherTimer();

        // Add the timer to the list to avoid it being garbage collected
        // after we exit the scope of the method.
        timers.Add(dispatcherTimer);

        EventHandler handler = null;
        handler = (sender, e) =>
        {
            // Stop the timer so it won't keep executing every X seconds
            // and also avoid keeping the handler in memory.
            dispatcherTimer.Tick -= handler;
            dispatcherTimer.Stop();

            // The timer is no longer used and shouldn't be kept in memory.
            timers.Remove(dispatcherTimer);

            // Perform the action.
            action();
        };

        dispatcherTimer.Tick += handler;
        dispatcherTimer.Interval = TimeSpan.FromSeconds(delay);
        dispatcherTimer.Start();
    }
}

จากนั้นคุณสามารถเรียกมันได้ดังนี้:

DelayedExecutionService.DelayedExecute(() => MessageBox.Show("Hello!"));

or

DelayedExecutionService.DelayedExecute(() => 
{
    DoSomething();
    DoSomethingElse();
});
person Adi Lester    schedule 09.09.2012
comment
ว้าว! สุดยอดทางออกครับท่าน!! มันทำงานได้อย่างสมบูรณ์แบบ!! ฉันจะคิดถึงการตอบสนองด้วยภาพ เช่น วงกลมที่ขยายใหญ่ขึ้นรอบๆ เคอร์เซอร์ หรืออะไรทำนองนั้น!! ขอบคุณมาก! - person Armando Freire; 09.09.2012
comment
@ArmandoFreire ดีใจที่ได้ยิน โปรดทราบว่าฉันได้แก้ไขโค้ดเพื่อหลีกเลี่ยงหน่วยความจำรั่วที่อาจเกิดขึ้น - person Adi Lester; 09.09.2012
comment
แน่นอน @AdiLester ฉันเพิ่งอัปเดตของฉันเช่นกัน! รหัสช่วยประหยัดเวลาได้มากที่นี่!! ขอบคุณจริงๆ! - person Armando Freire; 09.09.2012
comment
เฮ้ @Adi ฉันจะเพิ่มมากกว่าหนึ่งวิธีในกิจกรรมนั้นได้อย่างไร ฉันหมายถึงว่าฉันกำลังพยายามเรียกใช้สองวิธี (เช่น การเรียกกล่องข้อความสองกล่อง) แต่มันไม่ทำงาน ฉันต้องเรียกใช้ฟังก์ชันนั้นสองครั้งหรือไม่? - person Armando Freire; 15.09.2012
comment
@ArmandoFreire ผู้รับมอบสิทธิ์ที่คุณผ่านสามารถดำเนินการได้หลายคำสั่ง ฉันได้อัปเดตคำตอบเพื่อแสดงให้เห็นว่า - person Adi Lester; 15.09.2012
comment
มีวิธีทำเช่นเดียวกันในเทมเพลต XAML หรือไม่ ฉันกำลังพยายามสร้างแอนิเมชั่นเมื่อคลิกปุ่ม แต่ฉันไม่ต้องการให้เหตุการณ์ CLick เริ่มทำงานก่อนที่แอนิเมชั่นจะเสร็จสมบูรณ์ - person Stefan Vasiljevic; 19.07.2014
comment
@StefanVasiljevic ในกรณีนั้นคุณสามารถลงทะเบียนในเหตุการณ์ Completed ของกระดานเรื่องราวและดำเนินการตรรกะของคุณที่นั่น - person Adi Lester; 19.07.2014
comment
@StefanVasiljevic ใช่ครับ กิจกรรม Completed ของ Google - ฉันแน่ใจว่าคุณจะพบตัวอย่างมากมาย - person Adi Lester; 19.07.2014
comment
โปรดช่วยฉันด้วย ฉันกำลังพยายามใช้โค้ดนี้เพื่อชะลอเหตุการณ์การเปลี่ยนข้อความไม่ให้เริ่มทำงาน และฉันก็หลงทาง... - person Little Programmer; 19.07.2016
comment
ฉันต้องการที่จะหน่วงเวลาแม้กระทั่งจากการยิงทุกครั้งที่มันถูกเรียกระหว่างหนึ่งระหว่างการวิ่งหนึ่งครั้งเป็นเวลา 3 วินาที - person Little Programmer; 19.07.2016
comment
@LittleProgrammer คุณอาจกำลังมองหาการเชื่อมโยงล่าช้า: jonathanantoine.com/2011/09/21/ - person Adi Lester; 19.07.2016

ฉันแค่อยากจะเพิ่มวิธีแก้ปัญหาที่ง่ายกว่า:

public static void DelayedExecute(Action action, int delay = 3000)
{
    Task.Factory.StartNew(() => 
    {
        Thread.Sleep(delay);
        action();
    }
}

จากนั้นใช้มันเหมือนกับในคำตอบอื่น ๆ นี้

person Louis Kottmann    schedule 15.09.2012
comment
มันง่ายกว่าจริงๆ แต่ฉันคิดว่าควรหลีกเลี่ยง Sleep ทุกครั้งที่ทำได้ - person Adi Lester; 15.09.2012
comment
@AdiLester คุณช่วยแสดงความคิดเห็นว่าทำไม? - person Louis Kottmann; 15.09.2012
comment
บทความนี้ระบุเหตุผลหลายประการ: msmvps.com/blogs/peterritchie/archive/2007/04/26/ - person Adi Lester; 15.09.2012