K
K
Konstantin2018-09-14 22:53:19
C++ / C#
Konstantin, 2018-09-14 22:53:19

How to make a delay with an interval?

Tracking the hotkey pressed by the user. WndProc - monitors Windows messages (greater than 5 in 1 second). Problem: how to get rid of multiple clicks so that there is at least a delay or time interval when the SetPrc () method will be called? Otherwise, it turns out that you can call the method many times in 1 second. Is there any way to block the SetPrc() method via lock and Thread.Sleep() ?

protected override void WndProc(ref Message keyPressed)
        {
            if (keyPressed.Msg == 0x0312)
            {
                if (CanWork)
                {
                    switch (keyPressed.WParam.ToInt32())
                    {
                        case 1: SetPrtSc(); break;
                    }
                }
            }
            base.WndProc(ref keyPressed);
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
basrach, 2018-09-15
@basrach

It will be nice to do this:
1) Install the System.Reactive package
2) Add:

...
private Subject<int> myHotkey = new Subject<int>();

public MainForm()
{
    ...

    myHotkey
        .AsObservable()
        .Throttle(TimeSpan.FromSeconds(1))
        .Subscribe(hotkeyid => MessageBox.Show("Нажата горячая клавиша с ID:  " + hotkeyid));
}

...

protected override void WndProc(ref Message keyPressed)
{
    ...
            switch (keyPressed.WParam.ToInt32())
            {
                case 1: 
                {
                    SetPrtSc(); 
                    myHotkey.OnNext(keyPressed.WParam.ToInt32());
                }
                break;
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question