程序没有进入线程

Program is not stepping into the Thread

Thread 中的函数不工作。在调试模式下测试,程序只是跳过进入线程表达式。以我为例"ThreadStart action = ()=>{...",更多请看如下,

      DirectoryInfo directory = new DirectoryInfo(srcFile);
        FileInfo[] files = directory.GetFiles(name +  ".pdf").OrderByDescending(n => n.Name).ToArray(); 
           if (files.Length == 0) { return; }              
            foreach (FileInfo t in files)
            {
                _queue.Enqueue(t.Name);  //Insert files in to the concurrent queue.        

                    ThreadStart action = () =>
                    {
                        try
                        {
                            _semaphore.WaitOne();  // Semaphore 
                            string qfile;
                            while (_queue.TryPeek(out qfile))
                            {  
                              string srcFile = srcPath + "\" + qfile;
                              string desFile = desPath + "\" + qfile);

                                MoveFil(srcFile, desFile);    
                                PrintReport(desFile); 
                                _queue.TryDequeue(out qfile);
                            }                                    
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Moving and Printing error: " +                              
                                             e.Message);                                
                        }
                        finally
                        {
                            _semaphore.Release();
                        }
                    };
                    Thread thread = new Thread(action);
                    thread.Start();
                }

     private void PrintReport(string filePrint)
      {
        try
        {
            ProcessStartInfo info = new ProcessStartInfo();
            info.Verb = "print";
            info.FileName = filePrint;
            info.CreateNoWindow = true;
            info.WindowStyle = ProcessWindowStyle.Hidden;                

            Process p = Process.Start(info);
            p.CloseMainWindow();
            if (!p.HasExited) Thread.Sleep(5000);

        }
        catch (Exception e)
        {
            MessageBox.Show("Encounted a problem while printing! "+                                
                             e.Message);

        }
    }

当您要求调试器跨过以 "ThreadAction action = () ..." 开头的行时,您是在声明将在线程中执行的代码。你还没有执行它。调试器跳过整行,因为代码不是在此时执行,而是在您调用 "thread.Start()" 时执行。但是,调试器不知道 thread.Start 开始执行您的操作中的代码,因此它也只是越过该行。

为了单步执行线程中的代码,您必须在操作代码中设置断点。跨步是行不通的。

编辑:看起来您的所有线程都在等待信号量。这样做没有意义。您的代码要么是异步的,在这种情况下它不需要信号量,要么是同步的,在这种情况下它不需要线程。

edit2:看来你的原因不是多线程。这是一个代码示例,它使用 async/await 执行阻塞操作而不冻结您的 UI。请注意,您必须像这样调用 NotMultithreading:

await NotMultithreading();

并且每个调用 NotMultithreading 的函数都必须标记为异步,一直到您的按钮处理程序。

public async Task NotMultithreading(string name, string srcPath, string desPath)
{
    //Your code sample used this line
    //DirectoryInfo directory = new DirectoryInfo(srcFile);
    //But I suspect you should use srcPath, which is probably your bug.
    DirectoryInfo directory = new DirectoryInfo(srcPath);
    FileInfo[] files = directory.GetFiles(name + ".pdf");
    foreach (FileInfo t in files)
    {
        try
        {
            var desFile = Path.Combine(desPath, t.Name);
            t.MoveTo(desFile);
            await PrintReport(desFile);
        }
        catch (Exception e)
        {
            MessageBox.Show("Moving and Printing error: " + e.Message);
        }
    }
}

private Task PrintReport(string filePrint)
{
    ProcessStartInfo info = new ProcessStartInfo();
    info.Verb = "print";
    info.FileName = filePrint;
    info.CreateNoWindow = true;
    info.WindowStyle = ProcessWindowStyle.Hidden;

    return Task.Run(() =>
    {
        try
        {
            Process p = Process.Start(info);
            p.WaitForExit();
        }
        catch (Exception e)
        {
            MessageBox.Show("Encounted a problem while printing! " +
                                e.Message);

        }
    });
}