We have a joint office, but it is very rare that we both need to use it at the same time. If either of us are in and the door is open it signals we are available, but we each still wait for the person working to look up and acknowledge the person entering as the signal they are ready to talk.
If the door is closed it does usually mean we are either on a private call, or simply don't want to be distracted or disturbed (I hate being distracted when I am doing tax returns or financial things like that for example).
If the door is closed (and we really needed to ask something/get something) we both would either knock quietly and listen for a reply to indicate we can go in, or more usually open the door and peek in quietly to see what the situation is. But we certainly wouldn't barge in and immediately start talking.
We never actually talked it over, but I guess we both are just naturally respectful of each other.
The only exception would be in the case of a real emergency of course then it would be fine to charge in and immediately start explaining what it was.
OP I think your DH needs to understand that you need your space, and that he is being unreasonable by assuming that what he wants from you right at that moment (answers/advice etc) automatically trumps anything you might be doing or concentrating on.
Also was a bit concerned to hear his assumption you are doing things you don't want him to see. Is he a little insecure for some reason?
Is there a way to sit him down calmly and explain it to him without him getting defensive and accusing you of hiding things from him?
You could also, during that conversation explain to him that if he cannot 'remember' to respect you and to stop barging in and launching into whatever conversation he wants to have, you can help with that by putting a lock on the door. Although to be fair I think it would be a real shame if you have to resort to that in order to get him to respect your need for your own space and peace.