Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Some of the questions to consider are:</p> <ul> <li><p>Is your application multi-threaded? Is the thread[s] which write[s] the messages the same as the UI thread which reads the messages and populates the list box?</p></li> <li><p>How many messages per second and being created?</p></li> <li><p>How many times per second do you want to update the list box?</p></li> <li><p>What's the maximum number of messages over time? Do you display them all in the list box, or do you discard older ones?</p></li> <li><p>Do you want log the messages into a file as well (e.g. for tech support purposes)?</p></li> <li><p>Is the listbox (the UI element) the only place where messages are stored? Is the listbox always displayed, or can the user hide/destroy it?</p></li> <li><p>How sophisticated are the end-users? Can you ask them to use other (better) tools to view your log file, or do you need to implement a (relatively unsophisticated) viewer into your own UI?</p></li> </ul> <p>Sorry to answer your question with questions, but these are questions which need answering before you can say "is this the best approach?".</p> <hr> <blockquote> <p>it's not a multithreaded application, is a freaking small application that I don't want to overload.</p> </blockquote> <p>If the application isn't multi-threaded, then the 'simplest thing that could possibly work' would be to not bother with a list or queue; and instead, let your other classes append messages directly into the UI listbox element (perhaps via a delegate), for example:</p> <pre><code>class MyClass { Action&lt;string&gt; log; MyClass(Action&lt;string&gt; log) { this.log = log; } void Something() { //log a message log("Hello world!"); } } class MyForm { ListBox listBox = new ListBox(); MyClass myClass; MyForm() { //create a delegate which logs strings //by writing them to the ListBox Action&lt;string&gt; log = delegate(string s) { listBox.Items.Add(s); }; //pass this logger to classes which need to use it myClass = new MyClass(log); //test it myClass.Something(); } } </code></pre> <hr> <p>Here's similar code using a non-anonymous delegate, with some extra functionality as requested in one of your comments:</p> <pre><code>class MyForm { ListBox listBox = new ListBox(); MyClass myClass; MyForm() { //pass the Log action to classes which need to use it myClass = new MyClass(Log); //test it myClass.Something(); } ///logs strings by writing them to the ListBox void Log(string s) { if (listBox.Items.Count == 300) listBox.Items.RemoveAt(0); listBox.Items.Add(s); } } </code></pre> <hr> <blockquote> <p>I think I answer wrong the (6). I should be able to send messages even if the listbox has not been created yet... that's why I suggested a List or Queue</p> </blockquote> <p>In this case, you do need storage other than only the ListBox. Your suggestion would work. An alternative suggestion is:</p> <ul> <li>When you create your ListBox, populate it from the List (using the ListBox.Items.AddRange method)</li> <li>In the Log function, if the ListBox doesn't exist then write the message to the List, else if the ListBox does exist then write the message to the ListBox.</li> </ul> <p>Doing this <em>might</em> be (very slightly) simpler than defining, listening to, and firing an event.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload