使用foreach C#时的无限循环

时间:2015-01-09 17:16:09

标签: c# visual-studio-2010 foreach ms-word add-in

我正在使用c#和visual studio 2010创建一个单词加载项。

我做了一个简单的测试代码来接受所有文档修订,如下所示:

if (CurrentDocument.Revisions.Count > 0)
{
    Project.LoadingWindow.LoadingWindow loadingWindow = 
                new LoadingWindow.LoadingWindow("Updating...");
    loadingWindow.ProgressBarVisibility = Visibility.Collapsed;
    System.Windows.Threading.Dispatcher loadingWindowDispatcher = 
                loadingWindow.Dispatcher;

    BackgroundWorker worker = new BackgroundWorker();
    worker.WorkerReportsProgress = false;
    worker.WorkerSupportsCancellation = false;
    worker.DoWork += delegate
    {
        List<Revision> revisions = new List<Revision>();
        int count = 0;
        foreach (Revision rev in CurrentDocument.Revisions)
        {
             count++;
             revisions.Add(rev);
        }
        AcceptRevisions(revisions, loadingWindowDispatcher, loadingWindow);

        worker.Dispose();
        worker = null;
     };

     worker.RunWorkerAsync();
     loadingWindow.Show();
}

如果我在foreach之后放置断点,调试将无法到达。

CurrentDocument.Revisions.Count 618

但是使用int计数器,我可以看到计数很容易 9000!

我做错了什么?我对Visual Studio和C#有点新,但我似乎无法弄清楚可能发生的事情。

1 个答案:

答案 0 :(得分:1)

我在这里完成了一个完整的Word Interop NOOB。必须下载并安装VS的Office模板,以便我可以使用它。我也没有使用WPF,因此我在一个简单的WinForms表单中替换了BackgroundWorker和Label。

为什么您没有直接使用Application.ActiveDocument.Revisions

我在我的Word文档中添加了一个Button并填充了一个List,然后将其传递给Form:

    private void button1_Click(object sender, EventArgs e)
    {
        if (Application.ActiveDocument.Revisions.Count > 0)
        {
            List<Revision> revisions = new List<Revision>();
            foreach (Revision rev in Application.ActiveDocument.Revisions)
            {
                revisions.Add(rev);
            }

            frmAcceptRevisions accept = new frmAcceptRevisions(revisions);
            accept.ShowDialog();
        }
    }

这里是frmAcceptRevisions:

public partial class frmAcceptRevisions : Form
{

    private List<Revision> _Revisions = null;

    public frmAcceptRevisions(List<Revision> Revisions)
    {
        InitializeComponent();
        this._Revisions = Revisions;
        this.Shown += frmAcceptRevisions_Shown;
    }

    private void frmAcceptRevisions_Shown(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        for(int i = 0; i < _Revisions.Count; i++)
        {
            backgroundWorker1.ReportProgress(i + 1);
            _Revisions[i].Accept();
            System.Threading.Thread.Sleep(1000); // for illustrative purposes only
        }
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        label1.Text = "Applying Revision " + e.ProgressPercentage.ToString() + " of " + _Revisions.Count.ToString();
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Revisions Accepted: " + _Revisions.Count.ToString());
        this.DialogResult = System.Windows.Forms.DialogResult.OK;
    }

}

然后我运行了项目,在我的文档中键入了一些文本,打开了Track Changes,并进行了一系列修改。当我点击Button我的表单弹出时,我可以看到每个版本在文档中被接受和更改,我的表单更新了预期的进度(因为我在循环中硬编码了很长的延迟)。