WPF窗体调试阻塞

WPF窗体调试阻塞,wpf,debugging,Wpf,Debugging,我很抱歉英语不好,因为我不是本地人 首先,这里有一个预定义的WPF表单代码,每当我按下F11键,代码从一开始就被调试,调试过程在bw_ProgressChanged事件中停止。调试过程很容易跳过DoWork方法。为什么? 顺便说一下,代码运行正常 using System; enter code hereusing System.Collections.Generic; using System.ComponentModel; using System.Linq; using

我很抱歉英语不好,因为我不是本地人

首先,这里有一个预定义的WPF表单代码,每当我按下F11键,代码从一开始就被调试,调试过程在bw_ProgressChanged事件中停止。调试过程很容易跳过DoWork方法。为什么? 顺便说一下,代码运行正常

         using System;
enter code hereusing System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication2
{

    public partial class MainWindow : Window
    {
        public MainWindow() 
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = true;

            bw.DoWork += bw_DoWork;
            bw.ProgressChanged += bw_ProgressChanged;
            bw.RunWorkerCompleted += bw_RunWorkerCompleted;
            bw.RunWorkerAsync();

        }
        #region bw
        private BackgroundWorker bw = new BackgroundWorker();
        bool flg = true;
        int tempSec = 0;
        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            while (flg)
            {
                if (tempSec < DateTime.Now.Second)
                {
                    tempSec = DateTime.Now.Second;

                    bw.ReportProgress(tempSec);
                }
            }

          }

        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            txtSecondCounter.Text = "Done";
      /*      if (!e.Cancelled && e.Error == null)//Check if the worker has been cancelled or if an error occured
            {
                string result = (string)e.Result;//Get the result from the background thread
                lblAlarm.Text = result;//Display the result to the user
                lblCounter.Content = "Done";
                lblCounterTimes.Content = (++countTimes).ToString();
                m_Timer.Dispose();
                if ((bool)chkLoop.IsChecked)
                    btnStart_Click(btnStart, null);
            }
            else if (e.Cancelled)
            {
                lblAlarm.Text = "User Cancelled";
            }
            else
            {
                lblCounter.Content = "An error has occured";
            }
            btnStart.IsEnabled = true;
            */

        }
        private void bw_ProgressChanged( object sender, ProgressChangedEventArgs e)
        {

            txtSecondCounter.Text = e.ProgressPercentage.ToString();
         }


        #endregion

        private void btnPlayPause_Click(object sender, RoutedEventArgs e)
        {
            flg = !flg;
            if (flg)
                bw.RunWorkerAsync();

            if (btnPlayPause.Content.ToString() == "Pause")
                btnPlayPause.Content = "Play";
            else
                btnPlayPause.Content = "Pause";

        }
    }
}
 }
使用系统;
使用System.Collections.Generic在此输入代码;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfApplication2
{
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
已加载私有无效窗口(对象发送器、路由目标)
{
bw.WorkerReportsProgress=true;
bw.workersupport扫描单元=真;
bw.DoWork+=bw_DoWork;
bw.ProgressChanged+=bw_ProgressChanged;
bw.RunWorkerCompleted+=bw_RunWorkerCompleted;
RunWorkerAsync();
}
#区域bw
私有BackgroundWorker bw=新BackgroundWorker();
bool flg=真;
int tempSec=0;
私有void bw_DoWork(对象发送方,DoWorkEventArgs e)
{
while(flg)
{
if(tempSec
Xaml代码:

  <Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication2"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525"
    Loaded="Window_Loaded"
    >
<DockPanel Name="pnlMain" LastChildFill="False">

    <DockPanel DockPanel.Dock="Top" Height="40" Background="AliceBlue" LastChildFill="False">


    </DockPanel>

    <DockPanel DockPanel.Dock="Bottom" Height="40" Background="AliceBlue" LastChildFill="False">

        <TextBlock Name="txtSecondCounter" Width="100" Height="30" Background="Yellow">0</TextBlock>
        <Button Width="100" Name="btnPlayPause" Click="btnPlayPause_Click" DockPanel.Dock="Left">Pause</Button>
        <TextBox Width="100"></TextBox>
    </DockPanel>

</DockPanel>

0
暂停


看起来你的帖子大部分都是代码;请添加更多详细信息。

根据您的描述,我不完全确定您遇到了什么问题,但您确实意识到这部分DoWork代码只有在tempsec达到59之前才是正确的

if (tempSec < DateTime.Now.Second)
{
    tempSec = DateTime.Now.Second;
...
if(tempSec