2

EDIT: It is not a listbox. My mistake. It is a list view.

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.

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.

Here's the code I started with. The big catch: I need the "do fancy calculations" to be in the same thread as the calling events due to items out of my control.

EDIT: I know 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.

My biggest problem isn't creating the thread. It's that my "do fancy" has to be in the same thread.

    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...
    }

3
  • What platform are you using? Is this a windows forms app? Jan 22, 2009 at 18:03
  • It must not be winforms, because the ListBox control in windows forms does not behave as explained. Selecting a single item to deselect multiples only fires one SelectedIndexChanged event. Jan 22, 2009 at 18:08
  • Yes. It is winForms. I made an edit to the article. Thanks for catching that.
    – Jerry
    Jan 22, 2009 at 18:12

5 Answers 5

2

I remember solving this issue before:

A better way perhaps for you would be to put a minimal delay in your ItemSelectionChange Handler. Say -- 50ms. Use a timer, Once the selection changes, restart the timer. If the selection changed more than once within the delay period, then the original is ignored, but after the delay has expired, the logic is executed.

Like this:

public class SelectionEndListView : ListView
{
private System.Windows.Forms.Timer m_timer;
private const int SELECTION_DELAY = 50;

public SelectionEndListView()
{
   m_timer = new Timer();
   m_timer.Interval = SELECTION_DELAY;
   m_timer.Tick += new EventHandler(m_timer_Tick);
}

protected override void OnSelectedIndexChanged(EventArgs e)
{
   base.OnSelectedIndexChanged(e);

   // restart delay timer
   m_timer.Stop();
   m_timer.Start();
}

private void m_timer_Tick(object sender, EventArgs e)
{
   m_timer.Stop();

   // Perform selection end logic.
   Console.WriteLine("Selection Has Ended");
}
}
5
  • This is basically what I was doing (not in my sample hack above, but in my real code.) I was spawning a thread and letting it sleep. The problem is one of the controls (that I have no control over) requires that this data be in the same thread as itself.
    – Jerry
    Jan 22, 2009 at 18:21
  • Use a dispatching timer instead?
    – FryGuy
    Jan 22, 2009 at 18:52
  • You can only modify UI elements from the thread that created them. This is a rule for windows forms. You can switch to the UI thread by calling 'Invoke', but I think the timer class handles this automatically. Jan 22, 2009 at 18:53
  • HOLY CRAP! It does.... I did some testing, and it worked perfectly! I thought Timer put it in a new thread, but the picky control that I can't touch seems to be very happy with this.
    – Jerry
    Jan 22, 2009 at 21:12
  • The winforms timer fires events on the UI thread. If you're using WPF (I assumed you were for some reason), then you need to use the DispatchingTimer (fires on UI thread), as opposed to the System.Threading.Timer (fires on threadpool thread).
    – FryGuy
    Jan 23, 2009 at 0:11
1

A possible solution is to delay the work, so you know whether or not more events have fired. This assumes the order of the selections is not important; all that matters is the current state.

Instead of doing the work as soon as the event fires, set up a timer to do it a couple milliseconds after the event fires. If the timer is already running, do nothing. In this way the user should perceive no difference, but the actions will not hang.

You could also do the work on another thread, but have a flag to indicate work is being done. If, when the selection event fires, work is still being done you set a flag that indicates the work should be repeated. Setting 'repeat_work' to true 5000 times is not expensive.

1

I get the impression that you're trying to solve a problem through brute force. I would suggest trying a different event:

private void myListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
    if (e.IsSelected)
    {
        // do your logic here
     }
}

I would suggest avoiding creating threads if at all possible, since they have overheaad. I couldn't see from your example where there's any need for parallelism.

1
  • I am using SelectedIndexChanged. I SHOULD be using this event instead. Life just became MUCH easier. Thank you for the input!!
    – Jerry
    Jan 22, 2009 at 20:22
0

First, while you are properly synchronizing access to PaintQueue, I feel it was more by chance in this situation as opposed to design. If you have other code accessing PaintQueue on other threads, then you have a problem.

Second, this code makes no sense. You are spooling up a new thread, incrementing the value on that thread, and then waiting for 1/10th of a second. The thing is, the code that kicks off the thread is waiting on that thread to complete. Because of this, you are just waiting in the UI thread for nothing.

Even if you queue the SelectedIndexChange events, you aren't going to be able to prevent your app from hanging. The SelectedIndexChange event is going to fire every time that you select an item, and if the user selects 5000 items, then you need to process all 5000 events. You could give them a window (process every n seconds or whatever) but that's rather arbitrary and you put the user on a timer, which is bad.

What you should do is not tie the operation to the SelectedIndexChanged event. Rather, have the user select the items and then have them perform some other action (click a button, for example) which will work on the selected items.

Your app will still hang though if you have to process a number of items for a lengthy period of time on the UI thread, but at least selecting the items won't hang.

0

You don't really achieve any kind of concurrency by starting a new thread and then immediately Joining it. The only "effect" of the code above is that you method is run by another thread.

Additionally, if you want to use a background thread and safe the rather expensive cost of newing a thread, you should employ the thread pool.

3
  • Yes.. I posted the code as "here's what I'm trying to do... sort of." I know that this doesn't work because Join blocks the current thread which completely negates the purpose of having a thread. How do I get around that is my question.
    – Jerry
    Jan 22, 2009 at 18:26
  • Sorry, Jerry I missed that. I'll have another look then. Jan 22, 2009 at 18:34
  • Could you tell us a little bit about why you need the code to run on a specific thread? If it is because you have to access GUI controls, you can marshal your call to the GUI thread when needed. Otherwise please elaborate. Jan 22, 2009 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.