Application hang after screensaver

  • Thread starter mateusz.zajakala
  • Start date
M

mateusz.zajakala

Hello,

I have strange problem. I let my application work (it tests in loop
with 3sec brakes if it's possible to connect to remote pc /by ssh/)
and do nothing until screensaver activates. After it activates I move
mouse to get back to Windows. And now:
1. When I have screensaver protected with a password, whole
application hangs. It even blocks taskbar.
2. When I have screensaver without password, everything is just fine.
That's not the end. I've just found that, when I change my screen
settings (for example resolution, or wallpaper) application hangs too
(after confirming changes).

If you want me to post some code, please say which part of it I should
paste. It's quite big, so I don't want to spam too much.

Any ideas or articles about that issue would be very helpful.

Thanks in advice,
Mateusz M. Zaj±ka³a
 
M

mateusz.zajakala

I've just found that problem occure only when I start my application
by desktop shortcut.
Started 'normal' way behaves just fine.

Here's the way I create desktop shortcut:

private void desktopShortcut_Click(object sender, EventArgs e)
{
string desktopPath =
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

if (File.Exists(desktopPath + "/WokandaTools.lnk") == true)
{
MessageBox.Show("Shortcut already exists.", "Msgbox name",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
Shortcut.CreateShortcut();
File.Move(AppDomain.CurrentDomain.BaseDirectory + "\
\applicationName.lnk", desktopPath + "\\applicationName.lnk");
MessageBox.Show("Shortcut created.", "Msgbox name",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

public static void CreateShortcut()
{
string app = AppDomain.CurrentDomain.BaseDirectory +
@"\applicationName.exe";
try
{
IWshShell_Class shell = new IWshShell_ClassClass();
char znak = '.';
string appName = app.Remove(app.LastIndexOf(znak), 4);
IWshShortcut_Class shortcut = shell.CreateShortcut(appName +
".lnk") as IWshShortcut_Class;
shortcut.TargetPath = app;
shortcut.IconLocation = app + ",0";
shortcut.Save();
}
catch (COMException ex)
{
Console.WriteLine(ex.Message);
}
}
 
M

mateusz.zajakala

By 'normal' start I ment debug in VS. Started by .exe hangs everytime
I change my screen settings.
I'm sorry for little chaos I made, but I'm totaly confused. First time
get problem like this and I just can't find any answers or simillar
problems over net.

I would be gratefull for any ideas or help.
 
P

Peter Duniho

By 'normal' start I ment debug in VS. Started by .exe hangs everytime
I change my screen settings.

"Started by .exe" -- does that mean by _any_ shortcut or just
double-clicking the .exe file directly? Or is the shortcut-creation
code you posted required to create a shortcut that will reproduce the
problem?
I'm sorry for little chaos I made, but I'm totaly confused. First time
get problem like this and I just can't find any answers or simillar
problems over net.

Well, the problem you describe sounds unusual. Generally speaking,
having a screen-saver start, or having the workstation locked, should
not interfere with your program's operation.

That suggests that your program is doing something unique that is
leading to the problem. But you didn't post any code for anyone to look at.

You should post a concise-but-complete example of code that reliably
reproducees the problem. "Concise" means you include absolutely
_nothing_ that is not required in order to reproduce the problem;
complete means that you _do_ include everything else. The code sample
should be able to be compiled and run without any additional code or
other stuff added by someone trying to use it.

Pete
 
M

mateusz.zajakala

"Started by .exe" -- does that mean by _any_ shortcut or just
double-clicking the .exe file directly?

Generally. Starting application by .exe (directly or shortcut) couse
the problem. But after debbuging in VStudio everything works just fine
(_except_ screensaver with password. It couses the problem all the
time).
That suggests that your program is doing something unique that is
leading to the problem.

After little investigation I've found what couse the problem.
Monitoring connection with remote pc's done in loop hangs whole
application after screensaver, or just after changing any screen
settings (to be understood: right click on screen -> properties).
Without monitoring, application behaves normaly.
You should post a concise-but-complete example of code that reliably
reproducees the problem.

I'll try to do it now.

I have two forms. First - main form, second - monit form (kind of log,
checks and show result of testing connection with pc's).

In main form (mainForm_Load) I call monit form which is suppose to
work in background:

MonitConnection mc = new MonitConnection(listOfIpsToCheck);

In MonitConnection class I start the thread that checks connection.

public MonitConnection(List<string> IpList)
{
InitializeComponent();
t = new Thread(new ThreadStart(ThreadProc));
ipList = IpList;
t.Start();
}

Thread looks like this:

public void ThreadProc()
{
while (true)
{
//checkes if list of pc's (ip numbers) has changed
ipList = updateIpList();

if (ipList.Count == 0)
{
Thread.Sleep(5000);
continue;
}

for (int i = 0; i < ipList.Count; i++)
{
string host = ipList;

//here is use SharpSSH library to connect with pc's
SshConnectionInfo input = Util.GetInput(host);
SshExec shell = new SshExec(input.Host, input.User);
if (input.Pass != null)
shell.Password = input.Pass;
if (input.IdentityFile != null)
shell.AddIdentityFile(input.IdentityFile);

DateTime dt0 = DateTime.Now;

try
{
shell.Connect();
dt0 = DateTime.Now;
this.AddToList(dt0 + ": Connection " + ipList + " OK.");
shell.Close();
}
catch
{
notifyIcon.ShowBalloonTip(3000, "Information", "Connection
with " + ipList + " failed!", ToolTipIcon.Warning);
dt0 = DateTime.Now;
this.AddToList(dt0 + ": Connection " + ipList + "
failed.");
}

Thread.Sleep(5000);
}

Thread.Sleep(sleep);
}
}

Hope that clarify a bit.
Thank you for your answer,
Mateusz Zajakala
 
M

mateusz.zajakala

I've found the problem. It's little bit tricky for me, I'm very far
from expert in thread programming...

In thread method I'm checking if list of pc's (ip numbers) has
changed.

public void ThreadProc()
{
...
ipList = updateIpList();
...
}

public static List<string> updateIpList()
{
List<string> newIpList = null;

mainForm mf = new mainForm();
newIpList = mf.getIpList();
mf.Dispose(); //without this dispose applicaction hangs, after
adding this line it works good.

return newIpList;
}

Perhaps it's not too 'good-looking'. In main form I use getIpList()
function, so I thought I'll just refer to it. Perhaps I should repeat
declaration of that function in monitor class? Anyway with dispose it
works just fine.
But that don't answer me, why application hangs when i don't destruct
main form constructor. Any ideas?
 
P

Peter Duniho

[...]
Perhaps it's not too 'good-looking'. In main form I use getIpList()
function, so I thought I'll just refer to it. Perhaps I should repeat
declaration of that function in monitor class? Anyway with dispose it
works just fine.
But that don't answer me, why application hangs when i don't destruct
main form constructor. Any ideas?

I don't know specifically. But note that when the workstation is
locked, there's no desktop for the application to interact with. You
shouldn't be creating a form like that anyway, and it sounds as though
somehow the lack of a desktop causes something to get stuck, maybe
waiting on the processing of a window message that can never happen or
something like that.

Anyway, as I mention above, you shouldn't be creating the form in the
first place. Instantiating a form just because you want to call a
method on the form is very bad. You should only instantiate a form when
you actually want to show a new instance of that form. In all
likelihood you've already got one instance of that form, and that's the
only instance you should ever have.

You didn't post the code for mainForm.getIpList(), but it may not be the
sort of method that really needs to be in the form class in the first
place. The fact that the method works when you call it on a new
instance of the form suggests that there's not actually anything in that
method that uses the data in the form itself.

So, at the very least it seems like you could make the method a static
method, which would allow you to call it without an instance of the
form. It's likely that even better would be to move that method out of
the form class altogether; if it doesn't have anything to do with the
form itself, it's probably better to have it somewhere else (like in
your data management class).

Where to put the method and how to call it are two different issues.
Even if you move it, you'll want to make sure that it's either a static
method, or that you use a pre-existing instance of a class rather than
instantiating a class just so that you can call it.

Pete
 
M

mateusz.zajakala

Anyway, as I mention above, you shouldn't be creating the form in the
first place. Instantiating a form just because you want to call a
method on the form is very bad.
Ok.

You didn't post the code for mainForm.getIpList(), but it may not be the
sort of method that really needs to be in the form class

Trully? It does not needs to be in mainForm class or monitForm, but...
Is it better to refer to a method that belongs to the same class from
where I want to call it, or create instance of other class where that
method is and then call it? Isn't second method less efficent. I have
to create additional instance of class etc.
So, at the very least it seems like you could make the method a static
method, which would allow you to call it without an instance of the
form.

I wish, but within getIpList() method I use my own made structure
(list of classes) which holds data about pc's. So I can't make
getIpList static.

public List<string> getIpList()
{
List<string> ipList = new List<string>();

for (int i = 0; i < myStructure.Count; i++)
{
ipList.Add(myStructure.IpAdress);
}

return ipList;
}
Where to put the method and how to call it are two different issues.
Even if you move it, you'll want to make sure that it's either a static
method, or that you use a pre-existing instance of a class rather than
instantiating a class just so that you can call it.

Exacly. Like I asked isn't it better to hold method in same class from
which I want to call this method? Let's talk about situation when I
can't make my method static or use existing instance to call that
method.

Thank you for help,
Mateusz Zajakala
 
P

Peter Duniho

[...]
You didn't post the code for mainForm.getIpList(), but it may not be the
sort of method that really needs to be in the form class

Trully? It does not needs to be in mainForm class or monitForm, but...
Is it better to refer to a method that belongs to the same class from
where I want to call it, or create instance of other class where that
method is and then call it? Isn't second method less efficent. I have
to create additional instance of class etc.

No, it doesn't matter, and no you wouldn't create an additional instance
of the class. In fact, a problem with the code here is that you _are_
creating a new instance of the class. So it should be obvious I am not
suggesting that you create an instance method in a different class; if
the method is to be in a different class, it would be a static method
and you wouldn't need to create an instance to call it.

And as far as which class the static method is in, the call would be the
same either way. The only real difference here is whether the function
is static or not. A static function is (_very_ slightly) more efficient
because no "this" reference needs to be passed in the call. But which
class a static or instance method exists in is irrelevant as far as
performance goes.

(And frankly, the performance difference between static and instance
methods is so tiny it's not worth consideration IMHO; generally
speaking, whether to make a method static or not should be a design
decision, not a performance decision).
I wish, but within getIpList() method I use my own made structure
(list of classes) which holds data about pc's. So I can't make
getIpList static.

Well, when you create a new instance of your form class, how is that
structure initialized? Why can't you do that same initialization in the
static class when the getIpList() method is called?

The bottom line here is that you are creating a new instance of your
form class in order to call the getIpList() method, and so it is obvious
that up to the point at which you've created that instance, _none_ of
the work needed to implement getIpList() has been done. That is, no
code has executed up to that point that does any of the effort needed to
return the data you want.

Since that is true, then obviously you could just put all of that work
into a static method.

Now, is some of that work done in the "mainForm" class constructor?
Perhaps. Since you haven't posted any details about how any of that
work actually happens, it's hard to comment specifically.

But the fact that you're creating a new instance of your class just to
call the method proves that there's nothing in the total work done,
whether some of the work is in the constructor of the class or not, that
couldn't just all be done in the getIpList() method, without referring
to class instance members.

Now, this in fact may mean that you've got a buggy implementation and
that you didn't intend to repeat all of that work. It may mean that
what you _wanted_ to happen was to use a data structure that had already
been created. But that is definitely not what's going on here. If it
is what you wanted to happen, then you need to fix your code so that it
uses an existing instance of your form class, rather than creating a new
one.

Pete
 
M

mateusz.zajakala

And as far as which class the static method is in, the call would be the
same either way. The only real difference here is whether the function
is static or not. A static function is (_very_ slightly) more efficient
because no "this" reference needs to be passed in the call. But which
class a static or instance method exists in is irrelevant as far as
performance goes.

(And frankly, the performance difference between static and instance
methods is so tiny it's not worth consideration IMHO; generally
speaking, whether to make a method static or not should be a design
decision, not a performance decision).

That's what I wanted to know. Thanks for explenation.
you need to fix your code so that it uses an existing instance of
your form class, rather than creating a new one.

That's what I'll do. After your explenation I know exacly where I did
my mistakes and what to do with it.
So thank you for your time and answers.

Mateusz Zajakala
 

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