c# - Reading from FTP Server randomly returns a blank text file -


first, did try search answer problem failed find one.

i've been making program in local text file , online text file, located in ftp server, automatically updates each other. text file contains list of names. application designed use different machines simultaneously , can add , delete names in list.

with of additional algorithm, able merge local , online text files without causing problems when used offline , update when there internet connection (using additional local files).

now problem, works 90% of time. however, downloaded text file ftp server returns blank text file though not. noticed happen when application having hard time checking internet connection (pinging google.com). result total erasure of list since application interpret "the other user using application deleted list".

here method use download ftp server:

public boolean downloadfile(string uri, string path) {     ftpwebrequest request = (ftpwebrequest)webrequest.create(uri);     request.usepassive = true;     request.keepalive = true;     request.usebinary = true;     request.proxy = null;     request.timeout = 5000;     request.method = webrequestmethods.ftp.downloadfile;     request.credentials = new networkcredential(this.username, this.password);     try     {         ftpwebresponse response = (ftpwebresponse)request.getresponse();         stream responsestream = response.getresponsestream();         streamreader reader = new streamreader(responsestream);         stringbuilder sb = new stringbuilder();         sb.appendline(reader.readtoend());         file.writealltext(path, sb.tostring());         reader.close();         response.close();         return true;     }     catch (exception e )     {         messagebox.show("ftphandler download file:"+e.tostring());         return false;     } } 

the method used checking internet connection:

private static bool checkforinternetconnection()     {         try         {             ping myping = new ping();             string host = "google.com";             byte[] buffer = new byte[32];             int timeout = 15000;             pingoptions pingoptions = new pingoptions();             pingreply reply = myping.send(host, timeout, buffer, pingoptions);             return (reply.status == ipstatus.success);         }         catch (exception e)         {             //messagebox.show(e.tostring());             return false;         }     } 

this way call it. i've provided safety measures not happen still occur.

public void update() {    if(checkforinternetconnection()){      if (downloadfile(uri,path))      {        char[] charstotrim = { '\r', '\n' };        string onlinefile = file.readalltext(path).trimend(charstotrim);        if (onlineconfigfile.equals("") || onlineconfigfile == null)            messagebox.show("downloaded file empty");        //still stuff here regardless of message        //if other user intend delete list      }    } } 

the update method executed in different thread.

as first post here, sorry turned out long. did try make short didn't want miss details.

due nature of problem (random), think "lock" solution did fix me since problem did not occur after use quite time now. maybe because of threads, spawned every 5secs, caused problem processing different parts of method simultaneously.

here fixed code:

public void update() {    lock(lockobject)   {     if(checkforinternetconnection())     {       if (downloadfile(uri,path))       {         char[] charstotrim = { '\r', '\n' };         string onlinefile = file.readalltext(path).trimend(charstotrim);         if (onlineconfigfile.equals("") || onlineconfigfile == null)           messagebox.show("downloaded file empty");        //still stuff here regardless of message        //if other user intend delete list       }     }   } } 

i declared "lockobject" class level variable data type of "object".


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -