0

我和我的朋友一直在做一个游戏,我们设置了一个跟踪监听器,这里是一些代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms; 
using System.Diagnostics;

namespace GuiGame {

/// <summary>
/// A type of Trace Listener that sends its output to a ListBox.
/// </summary>
public class ListBoxTraceListener : TraceListener {

    private ListBox listBox;  // A reference to the listbox that we're writing to.

    private string stringToAddToListBox = "";

    private const char NEW_LINE = '\n';

    /// <summary>
    /// Parameterless constructor.
    /// Do not want the generic default constructor to be used
    /// as there is no way to set the ListBoxTraceListener's data.
    /// This replaces the compiler's generic default constructor.
    /// Pre:  none
    /// Post: ALWAYS throws an ArgumentException.
    /// </summary>
    /// <remarks>NOT TO BE USED!</remarks>
    public ListBoxTraceListener() {
        throw new ArgumentException("Parameterless constructor invalid.");
    } // end ListBoxTraceListener constructor

    /// <summary>
    /// Constructor with initialising parameters.
    /// Pre:  the existence of a ListBox on a GUI form.
    /// Post: initialised object.
    /// </summary>
    /// <param name="listBox">The ListBox that we're writing to.</param>
    public ListBoxTraceListener(ListBox listBox) {
        this.listBox = listBox;
    }

    /// <summary>
    /// Automatically collects the outputs from all Trace.WriteLine statements.
    /// Pre:  none.
    /// Post: the string s is displayed in the listBox.
    /// </summary>
    /// <param name="s"></param>
    public override void WriteLine(string s) {
        Write(s + NEW_LINE);
    } //end WriteLine

    /// <summary>
    /// Automatically collects the outputs from all Trace.Write statements.
    /// Pre:  none.
    /// Post: the string s is displayed in the listBox, once we receive a NEW_LINE.
    /// </summary>
    /// <param name="s"></param>
    public override void Write(string s) {
        stringToAddToListBox += s;

        // If we have one or more complete lines
        if (stringToAddToListBox.Contains (NEW_LINE)) {

            // Split the string into multiple lines. 
            // If NEW_LINE is found at the beginning or end of the string, 
            // then the corresponding array element contains an empty string. 
            string[] lines = stringToAddToListBox.Split(NEW_LINE);

            // Add all the lines to the listbox, except for the last one.
            // When stringToAddToListBox has a new-line at the end, 
            // the last element in lines[] will be an empty string.
            int highestLineNumber = lines.Length - 1;
            for (int i = 0; i < highestLineNumber; i++) {
                AddToListBox(lines[i]);
            }

            // Reset stringToAddToListBox to what remains. (May be an empty string).
            stringToAddToListBox = lines[highestLineNumber];
        }
    } // end Write

    /// <summary>
    /// Adds a complete output-line to the ListBox.
    /// Pre:  none.
    /// Post: the string listBoxLine is displayed in the listBox    .
    /// </summary>
    /// <param name="listBoxLine"></param>
    private void AddToListBox(string listBoxLine) {
        Debug.Assert(listBox != null, "listBox != null");
        listBox.Items.Add(listBoxLine);
    } // end AddToListBox
}

}

在这个阶段,我们只是尝试使用跟踪侦听器在其中输出一些文本,ListBox所以我们知道它正在工作,所以我们有一个事件处理程序设置:

private void RollDiceButton_Click(object sender, EventArgs e)
{

}

我们无法从跟踪侦听器获得任何输出。Add没有为此设置作为跟踪侦听器的方法。任何人都可以提供一些建议吗?我想也许我们正在做一些我们错过的非常愚蠢和明显的事情。

4

1 回答 1

0

问题的最可能原因是您的应用程序是单线程的(就像大多数简单的 Windows 应用程序一样)。这意味着尽管您正在向列表视图发送消息以将新元素附加到列表中,但该消息尚未处理,只是因为您的原始处理程序尚未返回(the RollDiceButton_Click)。

要解决此问题,您应该强制列表从当前处理程序中自行刷新:

private void AddToListBox(string listBoxLine) {
    Debug.Assert(listBox != null, "listBox != null");
    listBox.Items.Add(listBoxLine);
    // this would help? 
    listBox.Refresh();
} // end 

如果这没有帮助,请尝试暂时切换到无条件处理所有未决事件

private void AddToListBox(string listBoxLine) {
    Debug.Assert(listBox != null, "listBox != null");
    listBox.Items.Add(listBoxLine);
    // this would help? 
    Application.DoEvents();
} // end 

并报告回来。

于 2013-06-01T15:46:37.390 回答