public void UploadFileByBlocking(BacnetClient comm, BacnetAddress adr, BacnetObjectId object_id, string filename, Action <int> progress_action) { Cancel = false; //open file System.IO.FileStream fs = null; try { fs = System.IO.File.OpenRead(filename); } catch (Exception ex) { throw new System.IO.IOException("Couldn't open file", ex); } try { int position = 0; int count = comm.GetFileBufferMaxSize(); byte[] buffer = new byte[count]; while (count > 0 && !Cancel) { //read from disk count = fs.Read(buffer, 0, count); if (count < 0) { throw new System.IO.IOException("Couldn't read file"); } else if (count == 0) { continue; } //write to device if (!comm.WriteFileRequest(adr, object_id, ref position, count, buffer)) { throw new System.IO.IOException("Couldn't write file"); } //progress if (count > 0) { position += count; if (progress_action != null) { progress_action(position); } } } } finally { fs.Close(); } }