Cursor won't retain WaitCursor shape

S

scolemann

Hi all,

I am making a call to a webservice asynchronously, but would still like
to keep a WaitCursor on the screen until the service returns.
Unfortunately, after the initial call made to BeginWebServiceMethod
returns the cursor changes back to the Default cursor. I have tried
setting both Cursor.Current and this.Cursor on the form, but both have
the same behavior.

I tried a workaround someone posted catching WM_SETCURSOR, but it
doesn't work either (code below):

protected override void WndProc(ref Message m)
{
switch ((uint)m.Msg)
{
case Win32.WM_SETCURSOR:
{
if (this.Cursor == Cursors.WaitCursor)
Cursor.Current = this.Cursor;
else
base.WndProc (ref m);
return;
}
}
base.WndProc (ref m);
}

I don't have a full understanding of how windows handles cursors. What
message/event is causing the cursor to return to the Default cursor?
Is there a way I can capture this message and keep it from changing to
the default cursor?

Cole
 
C

Chris Dunaway

Do you call Application,.DoEvents at any point? Here is a quote from
the docs for Cursor.Current:

<Quote>
Note If you call Application.DoEvents before resetting the Current
property back to the Cursors.Default cursor, the application will
resume listening for mouse events and will resume displaying the
appropriate Cursor for each control in the application.
</Quote>
 
S

scolemann

No, I am not calling Application.DoEvents anywhere in my code. Maybe
it gets called somewhere in the framework?
 
R

Roger

I believe that the cursor will get reset every time the message loop is
allowed to run.

void onFoo()
{
Cursor = Cursors.WaitCursor;
DoSomething();
....
}

Even though this function doesn't reset the cursor back to the arrow,
the wait cursor will go away as soon as onFoo() exits. That's because
control of the thread has passed back to the framework's message loop.
Every time the message loop is executed, the wait cursor will go away.

To keep the wait cursor from going away, you need to either:

1) Block the thread with a lengthy operation or a Sleep command
2) Continuously reset the cursor inside the message loop. I've not
tried this approach in C#, so I don't know how easy it is...
 
C

Cole Shelton

Roger,

Thanks for the info. I realized this was the issue earlier today and
have been trying several workarounds. First I tried overriding WndProc
in my form and handling WM_SETCURSOR. The problem here is that the
WM_SETCURSOR is sent to the specific child window that has focus.
Therefore, I would need to override the WndProc of every child control.

I decided I would try adding a message filter via the
Application.AddMessageFilter. However, this is not working either
because the WM_SETCURSOR was not getting sent to the PreFilterMessage
method. Not sure why. When I watch the Thread using Spy++ I can see the
WM_SETCURSOR messages. Looking into it some more now. I'll post my
findings.

Thanks,
Cole
 

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