Locking Desktop and Threading Issue

J

Jenbo

Hi, I have an application that displays forms on the users desktop,
what I am doing with it is having a SystemEvents
SessionSwitchEventHandler to capture the lock of the desktop, I would
like to kill the forms when this event is captured, that piece is
fine, but I would like to sleep the app until the
SessionSwitchReason.SessionUnlock happens then I can wake it again and
go the correct part of the code? Can I wake a sleeping thread, or
should I not sleep the thread at all, is there another way to get the
thread to sleep or hibernate and then wake up?

Thanks
Eamonn
 
J

Jigar Mehta

Hello Jenbo,

I think you will be getting events whenever lock and unlock (of desktop)
happens. Do you want whole application to go to sleep (idle) at that time or
just one thread? If you wish that all threads go to sleep, you should have
one flag, being set whenever lock event is received, and being reset
whenever you get unlock. Other threads should check on that flags. there is
not any need to sleep really as you are getting events.

Regards,
Jigar Mehta
 
P

Peter Duniho

Hi, I have an application that displays forms on the users desktop,
what I am doing with it is having a SystemEvents
SessionSwitchEventHandler to capture the lock of the desktop, I would
like to kill the forms when this event is captured, that piece is
fine, but I would like to sleep the app until the
SessionSwitchReason.SessionUnlock happens then I can wake it again and
go the correct part of the code? Can I wake a sleeping thread, or
should I not sleep the thread at all, is there another way to get the
thread to sleep or hibernate and then wake up?

It's not clear to me that there's really any need to do any of the things
you want to do. What are your forms doing that you can't just leave them
open when the desktop is locked? Windows will take care of hiding them
from the user and preventing user input, and when the desktop is unlocked
then they will still be there, ready to go. Windows is also smart enough
to not dedicate any attention to the user interface of the application
while it's essentially inactivated by the desktop being locked (not that
the UI component of an application is generally a significant load on the
computer in the first place).

That said, it is possible to explicitly get a thread to simply stop doing
anything. Under Windows, a thread cannot "sleep" or "hibernate" per se
(not in the sense that the computer can do those things), but you can
create a waitable event and call the WaitOne() method on that. The thread
that calls that method will stop and wait at that call until some other
thread sets the event, releasing the thread for further execution.

If you use a ManualResetEvent, and you insert a call to the event's
WaitOne() method at some appropriate spot (generally a point in some
code's processing loop that is executed often enough that latency for any
pausing of the thread will be low, but not so often that calling WaitOne()
causes a significant performance problem), then when you want the thread
to pause you reset the event and when you want the thread to continue to
set the event.

Of course, you still need at least one thread around to process the
SessionSwitchEvent and manage any other threads that are blocked. But I
suppose if you have a thread that is doing something particularly i/o or
CPU intensive and you want it to stop taking system resources when the
desktop is locked, you could build in this sort of pausing mechanism
without affecting the main GUI thread.

If the above doesn't help you, perhaps you could be more explicit about
why it is you believe you need to "put your application to sleep" when the
desktop is locked. This isn't something that normally needs to be done,
so you should probably be more clear about what abnormal situation exists
in your scenario that justifies or demands that sort of behavior.

Pete
 
J

Jenbo

Thanks for the advice, basically my forms cover the full desktop, they
are survey forms that the user takes an action against, HR related
stuff. I can get the forms to cover the full desktop in normal
processing, I have a class that does this ok, but if for example the
user locks the desktop and the application creates a form when the
user unlocks the desktop the form does not show on the full desktop
and the windows taskbar is shown, I realise this is probably normal
behaviour for windows, this is why I wanted to sleep the application,
so it can no process the form and then when it wakes up again the
application can create the normal form covering all the windows
desktop. So if I sleep the thread I can then wake it and process as
normal when the session event is unlock. So maybe I need to let it
process as normal and go back to my windows handler for the covering
of the desktop and check that piece,

Thanks
Eamonn
 
P

Peter Duniho

[...] So maybe I need to let it
process as normal and go back to my windows handler for the covering
of the desktop and check that piece

I agree, that's likely to be a more fruitful approach. Though, it may
simply be that there's a better way to maximize your form.

Note that the real issue you're having is that your form changes size when
the desktop is locked. You don't really want to suspend processing per
se. You just think that doing so would have the side-effect of preventing
the form from changing size.

It's been years since I did anything with full-screen desktop applications
and I haven't played around with the various methods to accomplish that
recently. So I probably can't offer much direct advice. However, perhaps
if you explained the technique you're using to maximize your form on the
desktop and what exactly happens to it after the desktop is locked and
then unlocked, you might get some good advice from other people in this
newsgroup about how to do that correctly.

If you do that, I would suggest starting a new thread, since the title of
this thread has very little to do with what you're actually asking about.
You'll probably get better response by doing so, and the thread will be
easier to look up later if someone else runs into the same issue.

Pete
 
J

Jenbo

[...] So maybe I need to let it
process as normal and go back to my windows handler for the covering
of the desktop and check that piece

I agree, that's likely to be a more fruitful approach. Though, it may
simply be that there's a better way to maximize your form.

Note that the real issue you're having is that your form changes size when
the desktop is locked. You don't really want to suspend processing per
se. You just think that doing so would have the side-effect of preventing
the form from changing size.

It's been years since I did anything with full-screen desktop applications
and I haven't played around with the various methods to accomplish that
recently. So I probably can't offer much direct advice. However, perhaps
if you explained the technique you're using to maximize your form on the
desktop and what exactly happens to it after the desktop is locked and
then unlocked, you might get some good advice from other people in this
newsgroup about how to do that correctly.

If you do that, I would suggest starting a new thread, since the title of
this thread has very little to do with what you're actually asking about.
You'll probably get better response by doing so, and the thread will be
easier to look up later if someone else runs into the same issue.

Pete

Thanks for the ideas. Although I am not sure why the last paragraph
was necessary as I would say that the title of the thread describes
pretty well what I wanted to do at first and where I was going with
this then. Strange to get such a pedantic end of message.
 
P

Peter Duniho

Thanks for the ideas. Although I am not sure why the last paragraph
was necessary as I would say that the title of the thread describes
pretty well what I wanted to do at first and where I was going with
this then. Strange to get such a pedantic end of message.

I'm sorry you found it pedantic, and honestly I don't see where you get
that. My intent was to offer you advice as to how you could post a
question that is more likely to get the results you want.

Personally, I think you could've done just fine keeping your thoughts on
the matter to yourself. I don't see the point in bringing it up, but
since you did...

Frankly, I left out a whole paragraph providing advice about how your
original post really wasn't the right way to ask the question. Further
more, as I have pointed out and as it *seemed* that you had agreed with,
the correct solution here isn't actually to use any sort of threading API
to deal with the problem.

Since threading isn't relevant to the question at all, having a subject
that mentions threading is a problem if you are looking for for help on
the question. It may mislead people who might be able to help you into
thinking that they don't have anything to offer (if they are unfamiliar
with threading issues, for example). Additionally, since restoring your
form to full-screen after unlocking the desktop *is* the goal, the subject
of the thread ought to mention that.

All that said, you are obviously free to post your questions as you like.
I appreciate you letting me know that you don't find my input helpful;
saves me the trouble of putting any more time or effort into more of your
questions.

Thanks,
Pete
 
J

Jenbo

I'm sorry you found it pedantic, and honestly I don't see where you get
that. My intent was to offer you advice as to how you could post a
question that is more likely to get the results you want.

Personally, I think you could've done just fine keeping your thoughts on
the matter to yourself. I don't see the point in bringing it up, but
since you did...

Frankly, I left out a whole paragraph providing advice about how your
original post really wasn't the right way to ask the question. Further
more, as I have pointed out and as it *seemed* that you had agreed with,
the correct solution here isn't actually to use any sort of threading API
to deal with the problem.

Since threading isn't relevant to the question at all, having a subject
that mentions threading is a problem if you are looking for for help on
the question. It may mislead people who might be able to help you into
thinking that they don't have anything to offer (if they are unfamiliar
with threading issues, for example). Additionally, since restoring your
form to full-screen after unlocking the desktop *is* the goal, the subject
of the thread ought to mention that.

All that said, you are obviously free to post your questions as you like.
I appreciate you letting me know that you don't find my input helpful;
saves me the trouble of putting any more time or effort into more of your
questions.

Thanks,
Pete

As a matter of fact, what I wanted to do was threading,
Since threading isn't relevant to the question at all, having a subject
that mentions threading is a problem if you are looking for for help on
the question

It was totally relevant to the question but obviously you read my mind
and decided otherwise. Thanks again.
 

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