Please help me make this code thread safe

后端 未结 2 1800
终归单人心
终归单人心 2021-01-15 12:20

I\'ve got a bit of a problem with making my data loading and filtering thread safe.

The following code on my control\'s base class which handles all the data populat

2条回答
  •  北海茫月
    2021-01-15 12:53

    First, add a lock around your Populate method body:

    private object _exclusiveAccessLock = new object();
    public void Populate(bool reload)
    {
        lock (_exclusiveAccessLock)
        {
             // start the job
        }
    }
    

    This will help you avoid a race condition (although: if I got it right, since you are using a Windows.Forms Timer, it will always fire from the Gui thread, so they should never be executed exactly at the same time).

    Next, I am not sure if you should throw the exception at all. You can, for example, set an additional flag that shows you that the worker hasn't finished yet, but that is what IsBusy should tell you anyway.

    Then there is the m_BlockFilter flag. I cannot see where you are setting it from. It should be set inside the lock also, not in the background thread, because in that case you cannot be sure that it will not be delayed. You also need to make the field volatile if you are going to use it as a cross-thread flag.

提交回复
热议问题