问题
Scenario:
- 4 users launch separate instances of the same client program (Winforms) that is connected to a database based To-Do list.
- The first user selects the 3rd To-Do list item.
How do I update/refresh the other 3 users screens to reflect that item #3 is no longer available?
My thought was a table that contains the last update date-time stamp. Then a timer would check every few seconds to see if there have been any changes.
UPDATE1:
Thanks to all - there are definitely a number of valid answers.
I went with simpler version of the scenario that Icemanind recommended.
回答1:
Yes. The best way to do this is to implement a "push" type system. Here is how it would work. Whenever someone clicks something on the client end, the client would send a message to the server. The server would need to receive this signal and then the server would send out a refresh message to all clients connected to the server.
I don't know your client or server is coded, but you'll want to create a thread on the server that "listens" for incoming messages from clients and once it receives a message, puts it into a queue, the returns to listening for more messages. A second thread on the server needs to process the messages in the queue.
On the client side, you'll also want a second thread that listens for incoming messages from the server. Once a message is received, it can process the message and take whatever action is necessary.
A pretty decent tutorial on client/server and socket programming can be found here: http://www.codeproject.com/KB/IP/serversocket.aspx
Of course, it is a guide. You will need to modify it as you see fit.
Hope this makes sense and good luck!
回答2:
As Lucas suggested you can implement a 'Push' style system that whenever an entity is modified it is 'Pushed' to the other connected users. This can be a bit complex. Working with a legacy system the way we handle this is through a 'Change Number' column but really it can be anything that is updated each time the record is modified.
When a user attempts to modify an entity we query the database to row-lock that entity where the 'Change Number' reflects the 'Change Number' the user currently has.
If the lock is successful the user is able to update/delete the entity. When they are done they 'Save/Commit' and 'Change Number' on the entity is increased.
If they fail to get the row-lock and the 'Change Number' was the same, we display a message that the entity they requested is in use by another user. If the 'Change Number' was different then the message states they must refresh their view.
回答3:
You could implement a 'Push' system where when 1 user updates something, the server sends an update message to all connected clients.
回答4:
I'd opt for an isDirty timestamp/flag on the items. No need to get all the items again and no need to create a difficult push system. Reread the items since last call every now and then.
回答5:
If you're using SQL Server as your store, and ADO.NET or something on top of that for your data access, you should also check into SQL Dependency.
Basically, you can create a query from a table, and you can instruct SQL Server to notify you if any of the data in that query result changes.
See some intro articles on the topic:
- Using SqlDepedency for data change events
- SqlDepedency: creating a smarter cache
- Using SqlDependency to monitor SQL Database changes
- Query notifications in ADO.NET 2.0
It requires a bit of initial setup effort, but the huge advantage here is: you'll be notified automagically when something you're interested in (which is part of your query result set) changes, and you can react to that. No messy polling or anything....
来源:https://stackoverflow.com/questions/3311741/refreshing-windows-program-when-other-users-make-changes