Reputation: 3071
I have a winform application which needs to consume a web service. Web service checks in the database for any changes. If there are any changes in the database the winform application should be notified and will perform some tasks accordingly. How do I do that?
I thought of using timer in my winform application and after say every 5 min connect to a web service and check if new changes are been done in Database. Is there any other way for this?
Update:
I am posting the code here base on the answers:
class PerformTasks {
public static bool checkIfInProgress { get; set; }
public static void InitializeWebService()
{
try
{
Timer = new System.Timers.Timer(2000);
Timer.Elapsed += OnTimedEvent;
Timer.Enabled = true;
}
}
private static void callService()
{
using (var service = new WebServiceSoapClient())
{
checkIfInProgress = true;
task1();
task2();
popMessage();
checkIfInProgress = false;
}
}
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
if (checkIfInProgress == false)
{
callService();
}
}
private static void PpopMessage()
{
var form = new Form
{
StartPosition = FormStartPosition.Manual,
ShowInTaskbar = false,
Text = "Message",
Size = new Size(500, 200),
};
var screen = Screen.FromPoint(form.Location);
Label lblText = new Label();
lblText.Text ="Test Text";
lblText.Dock = DockStyle.Fill;
lblText.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
form.MaximizeBox = false;
form.Controls.Add(lblText);
form.Location = new Point(screen.WorkingArea.Right - form.Width, screen.WorkingArea.Bottom - form.Height);
form.Show();
}
Now everything works fine except 1 task i.e popMessage(code snippet on top). Here the form is opened but it appears to be loading always. Before using times it used to work fine. How can I handle it?
Upvotes: 2
Views: 203
Reputation: 608
SignalR allows you to implement Web Services to do this very thing, in real time (no need for a timer or delay between updates). It allows you to set up a persistent connection between client and server, the server is then able to send messages to the client at any point using a stack of transports; WebSockets, Server Sent Events, Forever Frame and Long Polling) based on support available in that order.
You could use SignalR to establish the connection and when something happens on the Server (such as the change in the database you mentioned) broadcast to all clients that need to be notified. eg.
Clients.All.notifyDatabaseChanged(args);
Upvotes: 0
Reputation: 13976
That's the only way especially if the web service is not WCF-based or if you can't afford to modify it.
If you're using a timer just make sure you use System.Timers.Timer and follow the instructions here so that the Elapsed
handler is executed on the UI thread. Moreover, when the timer ticks you should probably spawn a worker thread (or Task, or await on an async method) that makes the service call. You don't want your UI to be blocked while the service call is in progress.
If you have control over the web service, then you may want to explore WCF Duplex Services, which allow you to callback clients from within services.
Upvotes: 2