I have a C# class that uses a BackgroundWorker to poll a specific database table for changes. If something changes, it fires an event. Additionally, I have a class Token that has a handler for this event which basically sets the tokens state to "dirty".

Now I asked myself if there can be something better than polling the database every few miliseconds. I thought of making an asynchronous method in my token like

public async Task<bool> IsDirty()
{
    return await Something();
}

But what should I await here? My BackgroundWorker only has its Do_Work and RunWorkerCompleted methods. The BackgroundWorker doesn't need to check all the time, but at least when someone calls/awaits the IsDirty() method.

有帮助吗?

解决方案

Yes, there is something definitely better than polling the DB. Using SqlDependency in a Windows Application

You can create a dependency object where SQL Server will notify you that something has changed (a record inserted, updated, deleted).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top