Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing threads to count the loops in C# events
    primarykey
    data
    text
    <p>EDIT: It is not a listbox. My mistake. It is a list view.</p> <p>I have a list view control that's driving me nuts. It is a multi-select list box, so if the user selects 5000 rows, then de-selects them by selecting a single row, the SelectedIndexChanged fires 5001 times. This causes my app to hang.</p> <p>I'm trying to use threads to count the number of times that the event WOULD have fired, and then letting the last iteration do all the actual work.</p> <p>Here's the code I started with. <b>The big catch:</b> I need the "do fancy calculations" to be in the same thread as the calling events due to items out of my control.</p> <p>EDIT: I <em>know</em> that this code doesn't work. The Join() blocks the current thread which negates the entire purpose of creating the thread. My question is : How do I do something LIKE this.</p> <p>My biggest problem isn't creating the thread. It's that my "do fancy" has to be in the same thread.</p> <p><code></p> <pre><code> void IncrPaintQueue() { PaintQueue++; Thread.Sleep(100); } int PaintQueue = 0; private void SegmentList_SelectedIndexChanged(object sender, EventArgs e) { // We need to know how many threads this may possibly spawn. int MyQueue = PaintQueue; // Start a thread to increment the counter. Thread Th = new Thread(IncrPaintQueue); Th.IsBackground = true; Th.Start(); Th.Join(); // if I'm not the last thread, then just exit. // The last thread will do the right calculations. if (MyQueue != PaintQueue - 1) return; // Reset the PaintQueue counter. PaintQueue = 0; // ... do fancy calculations here... } </code></pre> <p></code></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.
 

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