(refactor): get rid of most thread sleep

This commit is contained in:
Maksym Sadovnychyy 2024-11-01 10:49:44 +01:00
parent 5734fa9043
commit 6b6c3a1c7f
2 changed files with 17 additions and 19 deletions

View File

@ -42,7 +42,7 @@ public class Application {
public void LoadTape(TapeDeviceHandler handler) { public void LoadTape(TapeDeviceHandler handler) {
handler.Prepare(TapeDeviceHandler.TAPE_LOAD); handler.Prepare(TapeDeviceHandler.TAPE_LOAD);
Thread.Sleep(2000); handler.WaitForTapeReady();
Console.WriteLine("Tape loaded."); Console.WriteLine("Tape loaded.");
} }
@ -54,7 +54,7 @@ public class Application {
public void EjectTape(TapeDeviceHandler handler) { public void EjectTape(TapeDeviceHandler handler) {
handler.Prepare(TapeDeviceHandler.TAPE_UNLOAD); handler.Prepare(TapeDeviceHandler.TAPE_UNLOAD);
Thread.Sleep(2000); handler.WaitForTapeReady();
Console.WriteLine("Tape ejected."); Console.WriteLine("Tape ejected.");
} }
@ -150,7 +150,7 @@ public class Application {
for (int i = 0; i < blocks; i++) { for (int i = 0; i < blocks; i++) {
handler.WriteData(new byte[blockSize]); handler.WriteData(new byte[blockSize]);
Thread.Sleep(100); handler.WaitForTapeReady();
} }
} }
@ -166,13 +166,13 @@ public class Application {
handler.SetMediaParams(blockSize); handler.SetMediaParams(blockSize);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND); handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000); handler.WaitForTapeReady();
handler.Prepare(TapeDeviceHandler.TAPE_TENSION); handler.Prepare(TapeDeviceHandler.TAPE_TENSION);
Thread.Sleep(2000); handler.WaitForTapeReady();
handler.Prepare(TapeDeviceHandler.TAPE_LOCK); handler.Prepare(TapeDeviceHandler.TAPE_LOCK);
Thread.Sleep(2000); handler.WaitForTapeReady();
handler.WaitForTapeReady(); handler.WaitForTapeReady();
@ -201,7 +201,7 @@ public class Application {
} }
handler.WriteData(buffer); handler.WriteData(buffer);
currentTapeBlock++; currentTapeBlock++;
Thread.Sleep(100); // Small delay between blocks handler.WaitForTapeReady();
} }
} }
@ -219,16 +219,16 @@ public class Application {
Array.Copy(descriptorData, startIndex, block, 0, length); Array.Copy(descriptorData, startIndex, block, 0, length);
handler.WriteData(block); handler.WriteData(block);
currentTapeBlock++; currentTapeBlock++;
Thread.Sleep(100); // Small delay between blocks handler.WaitForTapeReady();
} }
// write 3 0 filled blocks to indicate end of backup // write 3 0 filled blocks to indicate end of backup
ZeroFillBlocks(handler, 3, blockSize); ZeroFillBlocks(handler, 3, blockSize);
handler.Prepare(TapeDeviceHandler.TAPE_UNLOCK); handler.Prepare(TapeDeviceHandler.TAPE_UNLOCK);
Thread.Sleep(2000); handler.WaitForTapeReady();
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND); handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000); handler.WaitForTapeReady();
}); });
} }
@ -244,10 +244,10 @@ public class Application {
handler.SetMediaParams(blockSize); handler.SetMediaParams(blockSize);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND); handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000); handler.WaitForTapeReady();
handler.SetPosition(TapeDeviceHandler.TAPE_SPACE_FILEMARKS, 0, 1); handler.SetPosition(TapeDeviceHandler.TAPE_SPACE_FILEMARKS, 0, 1);
Thread.Sleep(2000); handler.WaitForTapeReady();
handler.WaitForTapeReady(); handler.WaitForTapeReady();
@ -291,9 +291,9 @@ public class Application {
handler.Prepare(TapeDeviceHandler.TAPE_UNLOCK); handler.Prepare(TapeDeviceHandler.TAPE_UNLOCK);
Thread.Sleep(2000); handler.WaitForTapeReady();
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND); handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000); handler.WaitForTapeReady();
return null; return null;
} }
@ -311,14 +311,14 @@ public class Application {
handler.SetMediaParams(descriptor.BlockSize); handler.SetMediaParams(descriptor.BlockSize);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND); handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000); handler.WaitForTapeReady();
handler.WaitForTapeReady(); handler.WaitForTapeReady();
foreach (var file in descriptor.Files) { foreach (var file in descriptor.Files) {
// Set position to the start block of the file // Set position to the start block of the file
handler.SetPosition(TapeDeviceHandler.TAPE_ABSOLUTE_BLOCK, 0, file.StartBlock); handler.SetPosition(TapeDeviceHandler.TAPE_ABSOLUTE_BLOCK, 0, file.StartBlock);
Thread.Sleep(2000); handler.WaitForTapeReady();
var filePath = Path.Combine(restoreDirectoryPath, file.FilePath); var filePath = Path.Combine(restoreDirectoryPath, file.FilePath);
var directoryPath = Path.GetDirectoryName(filePath); var directoryPath = Path.GetDirectoryName(filePath);
@ -358,7 +358,7 @@ public class Application {
} }
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND); handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000); handler.WaitForTapeReady();
}); });
} }

View File

@ -151,10 +151,8 @@ public partial class TapeDeviceHandler : IDisposable {
if (errorCode == 0) // Assuming 0 means success/ready if (errorCode == 0) // Assuming 0 means success/ready
{ {
isReady = true; isReady = true;
Console.WriteLine("Tape is ready.");
} }
else { else {
Console.WriteLine($"Tape not ready, status code: {errorCode}. Retrying...");
Thread.Sleep(1000); // Wait 1 second before checking again Thread.Sleep(1000); // Wait 1 second before checking again
} }
} }