0

I'm currently running a sandbox Web Server on my local machine & using my Application, I am grabbing the contents of a text file (currently single line) and displaying it back onto the main form.

When the Web Server is alive, the application will run normally, which is as expected. When the web server is down, the application refuses to open.. Let alone spawn the process.

I thought i was approaching the application correctly. I've used a try catch block in the event that the web server is down. The code is to follow:

public String UpdaterURL = "http://192.168.0.12/DestinyScreening/Updater/Revision.txt";

public int UpdaterVersion(int CurrentVersion)
        {
            try
            {
                var RemoteRevision = (new WebClient()).DownloadString(UpdaterURL);
                return Int32.Parse(RemoteRevision);
            }
            catch
            {
                var Date = DateTime.Now.ToString("yyyy-MM-dd");
                var Time = DateTime.Now.ToString("HH:mm");
                StreamWriter file = new StreamWriter("ErrorReports\\" + Date + ".txt",true);
                file.WriteLine(Time + " - Could Not Contact Update Server");
                file.Close();
                return CurrentVersion;
            }
        }

Now, when I say the application does not run. It essentially does run in Debug mode via Visual Studio.. I have swapped from Debug to Release and built the solution, which running the executable does not run.

I've trawled through the code provided, everything works as expected. It's just a complete baffle to me, as to why the application does not spawn a process when double clicking the exe?

So, the overall question. With everything which looks like is running as normal through debug mode on Visual Studio.. What could be going on behind the scenes which will stop a process from spawning?


I'm unable to trace this error as it's to do with the release build (double clicking the executable not running through Visual studio). Event Viewer shows the following (which is of no use):

 <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
 <System>
  <Provider Name="Application Error" /> 
  <EventID Qualifiers="0">1000</EventID> 
  <Level>2</Level> 
  <Task>100</Task> 
  <Keywords>0x80000000000000</Keywords> 
  <TimeCreated SystemTime="2015-10-13T22:54:20.000000000Z" /> 
  <EventRecordID>12976</EventRecordID> 
  <Channel>Application</Channel> 
  <Computer>Daryls8</Computer> 
  <Security /> 
  </System>
  <EventData>
  <Data>DestinyScreening.exe</Data> 
  <Data>1.0.0.0</Data> 
  <Data>561d8b7c</Data> 
  <Data>KERNELBASE.dll</Data> 
  <Data>10.0.10240.16384</Data> 
  <Data>559f3b2a</Data> 
  <Data>e0434352</Data> 
  <Data>000b3e28</Data> 
  <Data>20bc</Data> 
  <Data>01d1060a17c9bb47</Data> 
  <Data>C:\Users\Daryl\Documents\Visual Studio 2015\Projects\DestinyScreening\DestinyScreening\bin\Release\DestinyScreening.exe</Data> 
  <Data>C:\WINDOWS\SYSTEM32\KERNELBASE.dll</Data> 
  <Data>0ba5af44-c01c-4d4f-a318-855d84d4148e</Data> 
  <Data /> 
  <Data /> 
  </EventData>
  </Event>
Daryl Gill
  • 5,464
  • 9
  • 36
  • 69
  • Is this a console app? Winforms? – David Tansey Oct 12 '15 at 01:22
  • @DavidTansey I'm sorry for missing the tag, it's a Winform – Daryl Gill Oct 12 '15 at 01:22
  • It's certainly strange that it "does not run". Maybe it executes but fails quickly? Can you add some logging output to the application? Note also that your `catch` block is ignoring the *actual* exception. That sort of thing tends to make debugging difficult. – David Oct 12 '15 at 01:24
  • Do I understand correctly that it works properly in RELEASE mode if the WebServer is up -- and it does nothing in RELEASE mode if the WebServer is down? – David Tansey Oct 12 '15 at 01:24

1 Answers1

4

When you hit this line

var RemoteRevision = (new WebClient()).DownloadString(UpdaterURL); 

and the web server is down, the thread executing that code will block until the timeout value for WebClient is reached, then it should throw an exception.

If you are running this on your UI thread, your UI will freeze up while waiting for the timeout.

When you say

when I say the application does not run. It essentially does run in Debug mode via Visual Studio

I strongly suspect that you are checking for the remote version in your startup code, which runs on the UI thread. With the UI thread blocked, the application becomes unresponsive and appears not to run.

Instead, make use of DownloadFileAsync() and the async/await pattern.

Note that you may need to disable your main form, or certain menu options/buttons, until the async operation completes depending on the requirements of your application, to prevent the user from interacting with functionality before the RemoteRevision has been determined, or you determine that the website is down.

You can also change the timeout for WebClient if the default is too long for your needs.

UPDATE

The async check would allow your app to continue processing windows events while the check is happening. This means the UI remains responsive to user input.

In the normal case (web server is available, and the remote call is fast), you will know quickly whether it is OK to proceed or you want to update, so the user will not notice async.

In the case where the web server is down, the UI remains responsive to the user because the event loop is being processed. You probably want to disable most of your UI until the async call completes. That way, if the web server is not available or if the call just takes longer than expected, the user cannot start using the UI early. You will still be able to show a MessageBox (or selectively enable parts of your UI) to alert the user that a new version is to be downloaded. I think you can probably accomplish this along the lines of

Form1.Enabled = false; // Whatever your main form is called
// Make the async call here
if (isNewVersionAvailable)
{
    MessageBox.Show("Exiting now to update the app version.");
    // Do whatever you do to update the app, and exit this program
}
Form1.Enabled = true;
Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • I'm sorry for the extensive delay. What I'm trying to accomplish at the end of all this, is quite simply an updater which checks for a new version on start up. If there is a new version check the crc of remote files compared to the local files and download accordingly. The approach I saw was to call the problematic function on start up to alert the user that a updated version is ready. Then call out to another executable to handle downloads, would the async check cause problems in this situation? – Daryl Gill Oct 12 '15 at 23:13
  • Unfortunately, this still does not work. The debug build still runs, but the release build does not run (Even after clearing the bin/release folder) – Daryl Gill Oct 13 '15 at 22:51
  • Use a logging framework like NLog and write out to the log when you hit the early parts of processing, such as when Main() first runs, in the constructor of your first/primary form, and in the OnLoad event of that form. That may give you an idea as to where the RELEASE build is getting stuck. – Eric J. Oct 14 '15 at 00:41
  • I have managed to move past this error, instead of using DownloadFileAsync, I decided to use the `DownloadFile` which will download the file locally. Read file, then remove file. Which seems to work as expected. Using the Debug mode, there has been no uncaught exceptions thrown. So the reason for this to happen.. I kinda feel the solution i've provided is a hacky resolution to avoid complication. But it works – Daryl Gill Oct 14 '15 at 00:49