ファイル転送アプリケーション(サーバークライアント)を持っています...ファイルの送信中にキャンセルを有効にしたいです。
クライアントは、backgroundworkerによって機能するSendFileメソッドをキャンセルしてから、受信スレッドをキャンセルするコマンドをサーバーに送信します。
サーバーがこのコマンドを受信すると、Stopメソッドを呼び出しますが、その行でスタックします。network.Read(data、0、data.Length);
どうすればこのスレッドを中止して、network.Read(..)にとらわれることなく
最終的に進むことができますか?
前もって感謝します。
Thread thTransferFile = null;
void Start()
{
thTransferFile = new Thread(unused => ft.Receive(destPath, Convert.ToInt64(fileSize);
thTransferFile.Start();
}
void Stop()
{
thTransferFile.Abort();
}
public void Receive(string destPath, long fileSize)
{
using (fs = new FileStream(destPath, FileMode.Create, FileAccess.Write))
{
try
{
int count = 0;
long sum = 0;
data = new byte[packetSize];
while (sum < fileSize)
{
count = network.Read(data, 0, data.Length); //thread stucks in this line when i abort it
fs.Write(data, 0, count);
sum += count;
}
}
finally
{
network.Write(new byte[1], 0, 1); //tell client that the file transfer ends
network.Flush();
fs.Dispose();
if (Thread.CurrentThread.ThreadState == ThreadState.AbortRequested)
{
File.Delete(destPath);
}
}
}