Search code examples
c#mysqlbackgroundworker

Using a visual c# backgroundworker to update database?


This seems like it should be easy, but I can't quite find an explanation that fits what I'm looking for. Does anyone know of a way to access a database in the background of a form, and update controls on the form? For example, my visual C# application is loading some text boxes and labels from a MYSQL web server database. When someone updates the website database, I'd like for the changes to be automatically reflected in the C# application. I've tried using a timer, but the form locks up when the timer goes off and retrieves the data. The backgroundworker control looks promising, but I can't seem to get it to run more than once, and haven't had much luck at all even getting it to do what I want. Is the backgroundworker the way to go in my case? If so, can it be set so that it constantly hits the database like every 30 seconds or so? Any recommendations would be appreciated!

Thanks,


Solution

    • Start a completly separate Thread (System.Threading.Thread) on form load and have it poll the database, writing changes to variables (use lock()) and setting a flag if changes occurred.
    • In your form use a timer to check for the change flag (e.g. every 250ms) and if there are any, copy them from the variables to the form (again using lock() to avoid tearing)