For some reason my program needs to pause in a function, while still reacting to user input. This is currently done using DoEvents
in a loop.
Unfortunately my textbox
does not work properly while I am in the DoEvents
loop. I cannot type in it. It seems to get the KeyDown
and KeyUp
events OK, but the Text
property and the dispaly do not show what I am typing.
To make matters more mysterious it reacts to backspace and Ctrl+V to paste text, just typing in it does not work. It works again OK as soon as I stop the loop.
Any ideas?
Attached is an example demonstrating the problem. Just in case it matters I am using VB on VS2010.
This is the xaml file:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="200" Width="200">
<StackPanel>
<TextBox x:Name="txtTest" Text="Testing" Margin="10"/>
<Button x:Name="btnStartLoop" Content="Start DoEvents Loop"
Click="StartLoop_Click" Margin="10"/>
<Button x:Name="btnStopLoop" Content="Stop DoEvents Loop"
Click="StopLoop_Click" Margin="10"/>
</StackPanel>
</Window>
This is the xaml.vb file:
Class MainWindow
Private stopLoop As Boolean = False
Private Sub StartLoop_Click(ByVal sender As System.Object,
ByVal e As System.Windows.RoutedEventArgs)
stopLoop = False
LoopFunction()
End Sub
Private Sub LoopFunction()
While stopLoop = False
System.Windows.Forms.Application.DoEvents()
System.Threading.Thread.Sleep(1)
End While
End Sub
Private Sub StopLoop_Click(ByVal sender As System.Object,
ByVal e As System.Windows.RoutedEventArgs)
stopLoop = True
End Sub
End Class