简体   繁体   English

在没有Application.Idle的情况下在C#Windows Forms应用程序中检测空闲时间

[英]Detecting idle time in a C# Windows Forms application without Application.Idle

I have a strange issue that I'm not too sure on how to fix or address. 我有一个奇怪的问题,我不太确定如何解决或解决该问题。 I'm writing a mini text editor style application - RichTextBox editor. 我正在编写一个迷你文本编辑器样式的应用程序-RichTextBox编辑器。

I need to do some complex parsing after the selection changes - updating position, selection text and various other bits about the context of the text around the area. 选择更改后,我需要进行一些复杂的解析-更新位置,选择文本以及有关该区域周围文本上下文的其他各种信息。

As it takes a bit of processing I don't want it to fire each time the selection changes if the user is scrolling with their arrow keys. 因为这需要一些处理,所以我不希望如果用户使用箭头键滚动选择,则每次选择更改时都不会触发它。 I thought of using the Application.Idle, but it fires too regularly. 我考虑过使用Application.Idle,但是它会定期触发。 I tried a timer, but it may fire while the selection arrows are still moving. 我尝试了一个计时器,但是当选择箭头仍在移动时,它可能会触发。

What I was thinking of was a countdown timer sort of utility that will reset the timer each time the RichTextBox SelectionChanged event fires, then when the timer hits 500 ms or 1000 ms it will execute the complex processing runs. 我在想的是一种倒数计时器实用程序,它将在每次RichTextBox SelectionChanged事件触发时重置计时器,然后当计时器达到500毫秒或1000毫秒时,它将执行复杂的处理运行。

Does this sound like a good idea? 听起来像个好主意吗?

You should probably start your processing in its own thread when it takes too long. 花费的时间过长,您可能应该在自己的线程中开始处理。 As soon as you get new inputs you can stop the previous calculation and start with the new information again (so consider a cancel mechanism for your thread). 一旦获得新的输入,就可以停止先前的计算,并再次从新的信息开始(因此请考虑为线程取消机制)。

When your thread is done you have to check if its results are valid (the selecion did not change in the meantime). 线程完成后,您必须检查其结果是否有效(在此期间选择没有更改)。 Finally, you can "synchronize" the results of the calculation to the GUI, which is hopefully quick enough :) 最后,您可以将计算结果“同步”到GUI,希望它足够快:)

This does only work, when there is a certain amount of calculation that can be done without writing to the GUI ... I am not sure if you can implement it this way. 只有在无需写入GUI即可完成一定数量的计算时,此方法才起作用...我不确定是否可以通过这种方式实现它。 It depends on the type of your calculations. 这取决于您的计算类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM