工人直接跳过完成?

时间:2014-05-17 00:41:54

标签: c# .net wpf

我的工作人员似乎正在立即完成而没有运行工作方法。

BackgroundWorker videoWorker;
...
videoWorker = new BackgroundWorker();
        videoWorker.WorkerReportsProgress = true;
        videoWorker.DoWork += new DoWorkEventHandler(VideoWorker_Work);
        videoWorker.ProgressChanged += new ProgressChangedEventHandler(VideoWorker_ProgressChanged);
        videoWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(VideoWorker_WorkCompleted);
...
private void VideoWorker_Work(object sender, DoWorkEventArgs e)
    {
        Console.WriteLine("SIR");
        string[] imageLocations = Directory.GetFiles(videoFolderPath, "*.bmp");

        VideoFileWriter writer = new VideoFileWriter();
        writer.Open(videoFilePath, videoWidth, videoHeight, videoFPS);
        int counter = 0;

        foreach (string imageLocation in imageLocations)
        {
            Bitmap bmp = new Bitmap(imageLocation);
            writer.WriteVideoFrame(bmp);
            counter++;
            videoWorker.ReportProgress((int) Math.Floor((double) counter / imageLocations.Count()));
            Console.WriteLine("HI");
        }

        writer.Close();
        Console.WriteLine("THERE");
    }

    private void VideoWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressDialog.ProgressValue = e.ProgressPercentage;
    }

    private void VideoWorker_WorkCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        progressDialog.Close();
        Console.WriteLine("This ain't running biatch");
    }

它将用于将图像编译成视频。似乎是在跳过工作方法,我不知道为什么。

输出:

    'TimeLapse.NET.vshost.exe' (CLR v4.0.30319: TimeLapse.NET.vshost.exe): Loaded 'C:\Users\Ryan\documents\visual studio 2013\Projects\TimeLapse.NET\TimeLapse.NET\bin\Debug\TimeLapse.NET.exe'. Symbols loaded.
Step into: Stepping over non-user code 'TimeLapse.NET.App..ctor'
'TimeLapse.NET.vshost.exe' (CLR v4.0.30319: TimeLapse.NET.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Step into: Stepping over non-user code 'TimeLapse.NET.App.Main'
Step into: Stepping over non-user code 'TimeLapse.NET.App.InitializeComponent'
'TimeLapse.NET.vshost.exe' (CLR v4.0.30319: TimeLapse.NET.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework.Aero2\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.Aero2.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'TimeLapse.NET.vshost.exe' (CLR v4.0.30319: TimeLapse.NET.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemXmlLinq\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemXmlLinq.dll'. Cannot find or open the PDB file.
'TimeLapse.NET.vshost.exe' (CLR v4.0.30319: TimeLapse.NET.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework-SystemXml\v4.0_4.0.0.0__b77a5c561934e089\PresentationFramework-SystemXml.dll'. Cannot find or open the PDB file.
'TimeLapse.NET.vshost.exe' (CLR v4.0.30319: TimeLapse.NET.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\UIAutomationTypes\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationTypes.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
This ain't running biatch

编辑:这是它运行的地方

private void VideoStartButton_Click(object sender, RoutedEventArgs e)
    {
        if (videoFilePath != null && !videoFilePath.Equals(""))
        {
            if (videoFilePath != null && !videoFilePath.Equals(""))
            {
                videoHeight = Convert.ToInt32(VideoHeightInput.Text);
                videoWidth = Convert.ToInt32(VideoWidthInput.Text);
                videoFPS = Convert.ToInt32(FPSInput.Text);
                progressDialog = new Progress();
                progressDialog.Show();
                videoWorker.RunWorkerAsync();
            }
            else
            {
                System.Windows.MessageBox.Show("Please select a source folder.");
            }
        }
        else
        {
            System.Windows.MessageBox.Show("Please select a file to save.");
        }
    }

1 个答案:

答案 0 :(得分:3)

在您定义之后,您似乎并没有真正运行它:

var videoWorker = new BackgroundWorker { WorkerReportsProgress = true };
videoWorker.DoWork += VideoWorker_Work;
videoWorker.ProgressChanged += VideoWorker_ProgressChanged;
videoWorker.RunWorkerCompleted += VideoWorker_WorkCompleted;

videoWorker.RunWorkerAsync();

但是,由于您从WorkCompleted方法获取输出,或许您只是没有发布该部分代码,而DoWork事件实际上是在抛出异常

WorkCompleted事件中,您可以检查线程中是否抛出异常:

private void VideoWorker_WorkCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Error != null)
        MessageBox.Show(e.Error.ToString());
    else
        MessageBox.Show("Nothing to see here. Move along.");
}

或者,在DoWork事件中放置一个断点,并在执行期间逐步执行。