private void Loop(object t) { var transfer = (FileTransfer)t; int chunk_size = _tox.FileDataSize(transfer.FriendNumber); byte[] buffer = new byte[chunk_size]; while (true) { ulong remaining = _tox.FileDataRemaining(transfer.FriendNumber, transfer.FileNumber, 0); if (remaining > (ulong)chunk_size) { //read the next chunk, if we don't read anything, break; if (_stream.Read(buffer, 0, chunk_size) == 0) { break; } //keep trying to send the data if it fails while (!_tox.FileSendData(transfer.FriendNumber, transfer.FileNumber, buffer)) { int time = (int)ToxFunctions.DoInterval(_tox.Handle); Thread.Sleep(time); } } else { //send the last chunk of data buffer = new byte[remaining]; if (_stream.Read(buffer, 0, (int)remaining) == 0) { break; } _tox.FileSendData(transfer.FriendNumber, transfer.FileNumber, buffer); } } _stream.Dispose(); //notify our friend that we're done sending //we should wait for our friend to send ToxFileControl.Finished back to confirm that he received everything correctly (this is not covered in this example) _tox.FileSendControl(transfer.FriendNumber, 0, transfer.FileNumber, ToxFileControl.Finished, new byte[0]); }