"Cursors.WaitCursor" and threads

F

Frank Luker

Hello,

in my windows mobile application (CF 2.0, C#) I use
Cursor.Current = Cursors.WaitCursor
from main application and also from threads that I have started.

I can reset the cursor to default. But when an exception occurs (even if it
is handled by try/catch) I am not able to reset the wait cursor to default
anymore.

Does anybody know this problem or has a solution for me?

Best regards
Frank
 
G

Guest

I would mention that using the finally block is great if you are just logging
of errors. If you are displaying a msgbox then you will need to reset the
cursor in each catch before displying the msgbox.
 
M

Mathieu Cartoixa

Frank Luker a écrit :
in my windows mobile application (CF 2.0, C#) I use
Cursor.Current = Cursors.WaitCursor
from main application and also from threads that I have started.
I think it is a bad idea to manage this UI feature from another thread
than the main (UI) one.
I can reset the cursor to default. But when an exception occurs (even if it
is handled by try/catch) I am not able to reset the wait cursor to default
anymore.

Does anybody know this problem or has a solution for me?
If you stick to the UI thread, you could use try/catch/finally to
automatically restore the cursor after some treatment has occured.

Better still, you could use RAII
(http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization) to
manage the cursor. Define a class like this (consider this a template, I
have not checked for compilation) :

public sealed class CurrentCursor: IDisposable {
private Cursor _Original;

public CurrentCursor(Cursor current) {
_Original=Cursor.Current;
Cursor.current=current;
}

~CurrentCursor() {
Dispose(false);
}

public void Dispose() {
Dispose(true);
GC.SupressFinalize(this);
}

private void Dispose(bool disposing) {
Cursor.Current=_Original;
}
}

You could then just write :
using (new CurrentCursor(Cursors.WaitCursor))
{
// Do your lengthy stuff here
}

And your lengthy stuff could call methods that modify the cursor,
provided they use the same CurrentCursor technic. Obviously, this is not
a thread safe solution, hence the "stick to the UI thread" rule.

Mathieu Cartoixa
 
G

Guest

This is a good technique, but if you wish to display a msgbox within the
using statement, you still have to set the cursor back to default or else you
have a wait cursor while the msgbox is displayed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top