Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在一个线程工作时暂停另一个线程?_C#_Winforms - Fatal编程技术网

C# 如何在一个线程工作时暂停另一个线程?

C# 如何在一个线程工作时暂停另一个线程?,c#,winforms,C#,Winforms,这是我的部分代码 DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrives) { MessageBox.Show("Waiting for drive"); f1.scan(d.Name); } 我要做的是扫描每个驱动器。扫描功能位于另一个windows窗体中。当我在消息框上按ok时,扫描在C驱动器上开始。但是,在扫描过程中,消息框再次显示。如果我再次按ok,D驱动器也会与C

这是我的部分代码

DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
    MessageBox.Show("Waiting for drive");
    f1.scan(d.Name);
}
我要做的是扫描每个驱动器。扫描功能位于另一个windows窗体中。当我在消息框上按ok时,扫描在C驱动器上开始。但是,在扫描过程中,消息框再次显示。如果我再次按ok,D驱动器也会与C一起被扫描。如果我删除MessageBox.Show(),则不会发生任何事情。我猜这两个线程正在运行。 问题是我希望按顺序扫描所有驱动器。我尝试了Thread.Sleep(Timer.Infinite)来阻止这个线程。但随后该计划就停止了。 帮帮我

public void scan(string path)
{
    scanPath = path;
    //scanPath = scanPathTextBox.Text;
    FileAttributes attribs = File.GetAttributes(scanPath);

    if ((attribs & FileAttributes.Directory) == FileAttributes.Directory)
    {
        // Perform directory scan.
        Thread scanThread = new Thread(() =>
        {
            var scannedFiles = new List<Tuple<string, ScanResult, string>>();

            this.Invoke(new Action(() =>
            {
                scanProgressBar.Style = ProgressBarStyle.Marquee;
                // scanProgressBar.Value = i;
                statusLabel.Text = "Scanning...";
                logTextBox.Text = scanPath;
            }));

            _clamAV.ScanDirectory(scanPath,
                (file, result, virus) =>
                {
                    scannedFiles.Add(Tuple.Create(file, result, virus));

                    this.Invoke(new Action(() =>
                    {
                        logTextBox.Text = file;
                    }));
                });

            // Analyse results.
            var infectedFiles = scannedFiles.Where(f => f.Item2 == ScanResult.Virus);

            this.Invoke(new Action(() =>
            {
                logTextBox.Text = scanPath;
                threatInfoBox.AppendText(string.Format("{0} file(s) scanned, {1} infected\r\n",
                    scannedFiles.Count, infectedFiles.Count()));
                logTextBox.AppendText("\r\n");

                foreach (var file in infectedFiles)
                {
                    threatInfoBox.AppendText(string.Format("{0} infected with {1}\r\n", file.Item1, file.Item3));
                }

                scanProgressBar.Style = ProgressBarStyle.Continuous;
                scanProgressBar.Value = 100;
                statusLabel.Text = "Scan complete.";
                logTextBox.Text = " ";
                Form2 f2 = new Form2(this);
                this.Hide();
                f2.ShowDialog();
            }));
        });

        scanThread.Start();
    }
    else
    {
        // Perform file scan.
        Thread scanThread = new Thread(() =>
        {
            // Signal start of scan on UI thread.
            this.Invoke(new Action(() =>
            {
                scanProgressBar.Style = ProgressBarStyle.Marquee;

                statusLabel.Text = "Scanning...";
                //scanProgressBar.Value = i;
            }));

            string virusName = "";

            ScanResult scanResult = _clamAV.ScanFile(scanPath, ScanOptions.StandardOptions, out virusName);

            // Report scan completion.
            this.Invoke(new Action(() =>
            {
                statusLabel.Text = "Scan complete.";
                logTextBox.Text = " ";
                scanProgressBar.Value = 100;

                logTextBox.Text = scanPath;
                if (scanResult == ScanResult.Clean)
                    threatInfoBox.AppendText("File is clean.\r\n");
                else
                    threatInfoBox.AppendText("File is infected with " + virusName + ".\r\n");
                logTextBox.AppendText("\r\n");
            }));
        });

        // Begin scanning.
        scanThread.Start();
    }
}
公共无效扫描(字符串路径)
{
扫描路径=路径;
//scanPath=scanPathTextBox.Text;
FileAttributes attribs=File.GetAttributes(扫描路径);
if((attribs&FileAttributes.Directory)==FileAttributes.Directory)
{
//执行目录扫描。
线程扫描线程=新线程(()=>
{
var scannedFiles=新列表();
this.Invoke(新操作(()=>
{
scanProgressBar.Style=ProgressBarStyle.Marquee;
//scanProgressBar.Value=i;
statusLabel.Text=“扫描…”;
logTextBox.Text=扫描路径;
}));
_clamAV.ScanDirectory(扫描路径,
(文件、结果、病毒)=>
{
添加(Tuple.Create(文件、结果、病毒));
this.Invoke(新操作(()=>
{
logTextBox.Text=文件;
}));
});
//分析结果。
var infectedFiles=scannedFiles.Where(f=>f.Item2==ScanResult.Virus);
this.Invoke(新操作(()=>
{
logTextBox.Text=扫描路径;
threatInfoBox.AppendText(string.Format(“{0}个扫描文件,{1}已感染\r\n”,
scannedFiles.Count,infectedFiles.Count());
logTextBox.AppendText(“\r\n”);
foreach(受感染文件中的var文件)
{
AppendText(string.Format({0}感染了{1}\r\n',file.Item1,file.Item3));
}
scanProgressBar.Style=ProgressBarStyle.Continuous;
扫描进度条。值=100;
statusLabel.Text=“扫描完成。”;
logTextBox.Text=”“;
Form2 f2=新的Form2(本);
this.Hide();
f2.ShowDialog();
}));
});
scanThread.Start();
}
其他的
{
//执行文件扫描。
线程扫描线程=新线程(()=>
{
//在UI线程上发出开始扫描的信号。
this.Invoke(新操作(()=>
{
scanProgressBar.Style=ProgressBarStyle.Marquee;
statusLabel.Text=“扫描…”;
//scanProgressBar.Value=i;
}));
字符串virusName=“”;
ScanResult ScanResult=\u clamAV.ScanFile(scanPath,ScanOptions.StandardOptions,out virusName);
//报告扫描完成。
this.Invoke(新操作(()=>
{
statusLabel.Text=“扫描完成。”;
logTextBox.Text=”“;
扫描进度条。值=100;
logTextBox.Text=扫描路径;
if(scanResult==scanResult.Clean)
threatInfoBox.AppendText(“文件是干净的。\r\n”);
其他的
threatInfoBox.AppendText(“文件感染了“+virusName+”)\r\n);
logTextBox.AppendText(“\r\n”);
}));
});
//开始扫描。
scanThread.Start();
}
}

您可以尝试锁定扫描功能。另外,它不是线程安全的。 尝试以下方法:

class TestScan {

 private static Object oMutex = new Object();

 public void scan(){
   lock(oMutex){
     /* do work */
   }
 } // end of scan method.

 /***/

 }
如果有另一个线程已经在执行锁调用,则属于此方法的每个线程都将等待锁调用。它基本上只是一个简单的互斥体。
您可以阅读它。

我在您的代码中没有看到任何与线程相关的内容。不如像@Eser说的那样,摆出一个与线程无关的姿势。显示第二个消息框,因为C:的扫描已完成。要么它比你想象的要快,要么就是
scan
坏了。不管怎样,这都与线程无关,因此我正在删除标记。这段代码是winform的一部分,而scan()位于另一个表单(具有线程)中,因为扫描和messagebox同时显示,所以我认为线程正在运行。可以包括scan()方法,还是它的签名?@sanjay什么?你不显示
扫描
的代码,这样我们就可以看到问题的根源。这不应该是一个答案,而是一个评论。我知道这一点,但我只有在拥有50个声誉的情况下才能评论。如果我因为试图帮助别人而不断获得选票,我将永远无法达到目的。@RicardoRacciore如果你不断获得选票,你不认为你应该尝试另一种方法吗?限制是为了减少系统中的垃圾邮件数量。发布你现在确定的答案。50代表不需要很长时间就能实现。这不是我投的反对票,只是因为我不想阻止你进步。您应该等待到达所需的代表,然后再发表评论,一切都会好起来的。你知道,规则是有原因的。@sanjay然后制作一个可复制的小片段,你可以在这里发布。