スレッドを中止、一時停止、または再開する方法は?C#マルチスレッド.abort().suspend().reusme()
で実行時エラーが発生します。 Abort()
(オブジェクト参照がオブジェクトインスタンスに設定されていません)、.Resume()
および.Suspend()
にはObseleteエラーがあります。
私は私のRun()
で試しました。Thread.Sleep(1000)しかし、それは使用されたスレッドのインスタンスではないので、動作しません。
どうすればいいですか?
THX
CODE:
class FolderStats : IFolderStats
{
Thread MyThread = null;
private bool x;
string Rootpath;
List<string> MyList = new List<string>();
Folder FD = new Folder();
public void Connect(string rootpath)
{
Console.WriteLine(Statuses.Waiting);
Thread.Sleep(1000);
FD.Path = rootpath;
Rootpath = rootpath;
Console.WriteLine(Statuses.Connected);
Thread.Sleep(1000);
}
public void Start()
{
MyThread = new Thread(Run);
MyThread.Start();
Console.WriteLine("Starting the Search");
}
public void Stop()
{
MyThread.Abort();
Console.WriteLine("Console Aborted. Please press Enter to Exit");
}
public void Pause()
{
this.x = false;
PauseResume();
Console.WriteLine("Console Paused.");
}
public void Resume()
{
this.x = true;
PauseResume();
Console.WriteLine("Console Resumed.");
}
private void PauseResume()
{
while (this.x == false)
{
Thread.Sleep(100);
}
}
public void Run()
{
MyThread = new Thread(Start);
if (!MyList.Contains(Rootpath))
{
MyList.Add(Rootpath);
var subDirs = Directory.GetDirectories(Rootpath, "*");
var Data = Directory.GetFiles(Rootpath, "*");
foreach (string dir in subDirs)
{
Thread.Sleep(2000);
Rootpath = dir;
Console.WriteLine(dir);
Run();
}
foreach (string file in Data)
{
Thread.Sleep(2000);
if (!MyList.Contains(file))
{
MyList.Add(file);
Console.WriteLine(file);
}
}
}
FD.NumberOfFiles = MyList.Count;
}
スレッドを中止、一時停止または再開しないでください。何を解決しようとしているのですか? – Dennis
Start()を呼び出した後、バックグラウンドでデータ収集を行う必要があります。停止または一時停止していつでも再開できます。 – k9nneo1191
も参照してください。http://www.albahari.com/threading/ – rene