Cursors and threads
-
In my main App I run a long running process on a seperate thread so as not to tie up the GUI - while the process is running I want to set the Cursor to the WaitCursor. The first thing the long running process does it fire an event which is handled on the GU thread and sets the cursor to the WaitCursor then when the process has finished it fires another event which is again handled on the GUI thread and sets the Cursor to default - however, the cursor never actually changes to the WaitCursor The long running process fires off an event every 1000 records or so which is handled on the GU thread thus:
Private Delegate Sub xxxDelegate(ByVal sender As Object, ByVal e As xxxEventArgs) Private Sub xxx(ByVal sender As Object, ByVal e As xxxEventArgs) Handles LongProcess.xxx If button.InvokeRequired = False Then Console.WriteLine("A - " & Cursor.Current.ToString()) ... Else Console.WriteLine("B - " & Cursor.Current.ToString()) Dim del As New xxxDelegate(AddressOf xxx) Me.BeginInvoke(del, New Object() {sender, e}) End If End Sub
When run the following is written to the console: B - [Cursor: WaitCursor] A - [Cursor: Default] B - [Cursor: WaitCursor] A - [Cursor: Default] B - [Cursor: WaitCursor] A - [Cursor: Default] ... Why is this? -
In my main App I run a long running process on a seperate thread so as not to tie up the GUI - while the process is running I want to set the Cursor to the WaitCursor. The first thing the long running process does it fire an event which is handled on the GU thread and sets the cursor to the WaitCursor then when the process has finished it fires another event which is again handled on the GUI thread and sets the Cursor to default - however, the cursor never actually changes to the WaitCursor The long running process fires off an event every 1000 records or so which is handled on the GU thread thus:
Private Delegate Sub xxxDelegate(ByVal sender As Object, ByVal e As xxxEventArgs) Private Sub xxx(ByVal sender As Object, ByVal e As xxxEventArgs) Handles LongProcess.xxx If button.InvokeRequired = False Then Console.WriteLine("A - " & Cursor.Current.ToString()) ... Else Console.WriteLine("B - " & Cursor.Current.ToString()) Dim del As New xxxDelegate(AddressOf xxx) Me.BeginInvoke(del, New Object() {sender, e}) End If End Sub
When run the following is written to the console: B - [Cursor: WaitCursor] A - [Cursor: Default] B - [Cursor: WaitCursor] A - [Cursor: Default] B - [Cursor: WaitCursor] A - [Cursor: Default] ... Why is this?First, since your long running thread doesn't do anything with the GUI, it should not be the thread that changes the cursor. This should be handled by the GUI code just before it launches the long thread. When the thread completes, some kind of event or signal is raised on the GUI thread that started the long thread. Then with signal occurs, the GUI thread handles changing the cursor back to normal. GUI effects only on the GUI thread! If your long running thread were to be used in a console app, how would it know that there was no cursor to change? Get the idea yet? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
First, since your long running thread doesn't do anything with the GUI, it should not be the thread that changes the cursor. This should be handled by the GUI code just before it launches the long thread. When the thread completes, some kind of event or signal is raised on the GUI thread that started the long thread. Then with signal occurs, the GUI thread handles changing the cursor back to normal. GUI effects only on the GUI thread! If your long running thread were to be used in a console app, how would it know that there was no cursor to change? Get the idea yet? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome