dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: How to abort a thread waiting on a lock???

  1. #1
    Join Date
    Apr 2004
    Posts
    123

    How to abort a thread waiting on a lock???

    I have a requirement wherin i have a timer which raises events in a timespan of 5 milliseconds..and i have got a pice of code in the timercallback which basically retrieves data from the database,updates a dictionary and gives it back to the UI for processing.

    Since the timeout is for 5 milliseconds,before finishing with the data loading part multiple threads seems to access the critical section(fetching data and updating dictionary) of the code.So,i did put a lock on the critical section and it works.But,my requirement is that i have to show the latest data on to the GUI,so i can ignore all the threads which are waiting on the lock till the lock aquired thread finishes processing.How should i tell all the threads which are waitng on a lock,not to wait on it but rather die itself??

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: How to abort a thread waiting on a lock???

    Code:
    private bool updating;
    public void UpdateFromDatabase
    {
        lock (whatever)
        {
            if (updating)
                return;
    
            updating = true;
        }
        
        GetDataFromDatabase();
        LoadDataIntoGrid();
        updating = false;
    }
    That'd do it fine.

    However, updating every 5ms from a database is insanity. No-one can process data which updates every 5ms. You're requirement might be to show the latest possible data, but updating every 2 seconds would be just as fast as updating every 5ms when a human is trying to look at the data. Even every 5 seconds would probably be fine.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)