程序没有进入线程

时间:2017-07-11 17:51:52

标签: c# .net multithreading threadpool

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);

        }
    }

1 个答案:

答案 0 :(得分:2)

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

要遍历线程中的代码,必须在操作代码中设置断点。单步执行不起作用。

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

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

await NotMultithreading();

每个调用NotMultithreading的函数都必须标记为async,一直到你的按钮处理程序。

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);

        }
    });
}