c# - What is an efficient way of knowing when an external variable changes? -


i'm dealing visual studio solution has 2 projects. main wpf project, , class library makes use of external com library , simplifies code easier call methods. i'm trying keep code separated, class library worker project, while main project has ui related code. problem while working variables inside class library.

i have variable on class library holds int=0 changes 1 if physical button pressed on scanner purchased. way have of knowing if button pressed checking variable property, , need way know when changes. idea have far doing:

thread backgroundthread = new thread(() => {    while (variable == 0) {         // work, if not;    thread.sleep(250);     } }); 

but feels inefficient, or feel. if modify property love add code set method notified of changing, since external com library's property have no control on them. way can this?

in general, there 2 ways know when has changed:

  • pull - pull value @ specific intervals , keep track of value between calls. if value has changed, perform "value changed" logic.

  • push - tell owner of value let know when value has changed. tell owner how want notified. owner push notification when value has changed, calling "value changed" logic you.

you're using pull method, , you're right think less efficient push method. however, since code not in position intercept writes target variable, pulling pretty can do.

double check api make sure there isn't kind of "callback" or "event" listed related variable you're interested in. if there is, can hook library there , pushed notifications of when value changes.

if there no way library push changes code, need decide how want poll value. if there isn't cost performing pull, i'd current 250 ms should responsive enough users feel value being updated in real time.

further reading:

how server-client push/pull connections work?

data-pull model , data-push model


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -