4

I have a function where I want to execute in a separate thread avoiding two threads to access the same resources. Also I want to make sure that if the thread is currently executing then stop that thread and start executing the new thread. This is what I have:

volatile int threadCount = 0; // use it to know the number of threads being executed
private void DoWork(string text, Action OncallbackDone)
{
    threadCount++;
    var t = new Thread(new ThreadStart(() =>
    {
        lock (_lock) // make sure that this code is only accessed by one thread
        {
            if (threadCount > 1) // if a new thread got in here return and let the last one execute
            {
                threadCount--;
                return;
            }
            // do some work in here
            Thread.Sleep(1000);
            OncallbackDone();
            threadCount--;
        }
    }));
    t.Start();
}

if I fire that method 5 times then all the threads will be waiting for the lock until the lock is released. I want to make sure that I execute the last thread though. when the threads are waiting to be the owner of the lock how can I determine which will be the next one owning the lock. I want them to own the resource in the order that I created the threads...

EDIT

I am not creating this application with .net 4.0 . Sorry for not mentioning what I was trying to accomplish. I am creating an autocomplete control where I am filtering a lot of data. I don't want the main window to freeze eveytime I want to filter results. also I want to filter results as the user types. If the user types 5 letters at once I want to stop all threads and I will just be interested in the last one. because the lock blocks all the threads sometimes the last thread that I created may own the lock first.

Brian Gideon
  • 47,849
  • 13
  • 107
  • 150
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • I think I would try to design it around *one* thread (or BackgroundWorker) that supports being able to "change course" or "abandon itself"... multiple threads sounds hairy and there is no point to keep working on previous queries if a later one comes in. –  Apr 15 '12 at 03:42

3 Answers3

4

I think you are overcomplicating this. If you are able to use 4.0, then just use the Task Parallel Library. With it, you can just set up a ContinueWith function so that threads that must happen in a certain order are done in the order you dictate. If this is NOT what you are looking for, then I actually would suggest that you not use threading, as this sounds like a synchronous action that you are trying to force into parallelism.

If you are just looking to cancel tasks: then here is a SO question on how to cancel TPL tasks. Why waste the resources if you are just going to dump them all except for the last one.

If you are not using 4.0, then you can accomplish the same thing with a Background Worker. It just takes more boilerplate code to accomplish the same thing :)

Community
  • 1
  • 1
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • I am actually filtering data as the user types data. I don't want the main thread to freeze therefore I executed it in a separate thread. If the user calls that method 5 times I am just interested in the last one but with the last example because of the lock the program will have to wait for each one to finish and execute one per time. – Tono Nam Apr 15 '12 at 03:29
  • @TonoNam You can still use the TPL, just cancel the current task on each new click. I have updated my answer to include cancelling – Justin Pihony Apr 15 '12 at 03:31
  • @TonoNam I saw your update about not using 4.0. I have updated my answer to suggest using a BackgroundWorker to accomplish the same idea – Justin Pihony Apr 15 '12 at 04:32
0

I agree with Justin in that you should use the .NET 4 Task Parallel Library. But if you want complete control you should not use the default Task Scheduler, which favors LIFO, but create your own Task Scheduler (http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.aspx) and implement the logic that you want to determine which task gets preference.

Using Threads directly is not recommended unless you have deep knowledge of .NET Threading. If you are on .NET 4.0; Tasks and TPL are preferred.

Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73
0

This is what I came up with after reading the links that you guys posted. I guess I needed a Queue therefore I implemented:

volatile int threadCount = 0;
private void GetPredicateAsync(string text, Action<object> DoneCallback)
{          
    threadCount++;
    ThreadPool.QueueUserWorkItem((x) =>
    {                
        lock (_lock)
        {
            if (threadCount > 1) // disable executing threads at same time
            {
                threadCount--;
                return; // if a new thread is created exit. 
                        // let the newer task do work!
            }
            // do work in here

            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                threadCount--;                        
                DoneCallback(Foo);
            }));

        }
    },text);
}
Brian Gideon
  • 47,849
  • 13
  • 107
  • 150
Tono Nam
  • 34,064
  • 78
  • 298
  • 470