mardi 7 avril 2015

Why does some code only work when I create/run a form in C#?

In the process of turning an application into something more like a background task, I noticed some odd behavior. It works fine when I run something like this (similar to the old code I'm modifying):



using (Foo f = new Foo(stuff)) {
f.doSomething();

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form());
}


The first step towards making this run in the background was of course to remove the references to the form/rendering:



using (Foo f = new Foo(stuff)) {
f.doSomething();
}


That way doesn't work, however. When I run that, a library that I use (which doesn't have anything to do with Windows forms) This is without any of the other changes I planned to do whatsoever. Only removing those three lines. If it is relevant, Foo in the actual code is a class that creates an SSH tunnel (using the SSH.NET library), but it is not referenced (directly or indirectly) through any code in the form/designer, it just needs to be set up/torn down so a connection can be made.


The error occurs before the connection to the tunnel is made, but instead when trying to connect to the remote host and forward the ports. The library reports a connection is made (IsConnected is true), but when port forwarding is started, it reports an exception with the message "Session is not connected"


Aucun commentaire:

Enregistrer un commentaire