Click here to Skip to main content
15,894,546 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
Now I write Windows Form application using C#. This app runs and writes data offline. Each 5 minutes, this app updates data to server.

How i can do that???
Posted
Updated 4-Nov-14 17:47pm
v2
Comments
[no name] 4-Nov-14 23:48pm    
Please try your own first.

Note that System.Timers.Timer is only one of several possible timers you can use in WinForms. I suggest you read this CodeProject article, and download/study/run its code samples, to review the different timers: "All about .NET Timers - A Comparison" [^].

This example uses a System.Timers.Timer to call a function regularly:
C#
// timer update interval in minutes
private const int UpdateServerInterval = 5;

// timer update interval in milliseconds
private Int64 msToUpdate = new TimeSpan(0,UpdateServerInterval,0).Milliseconds;

private System.Timers.Timer UpdateServerTimer = new System.Timers.Timer;

private delegate void updateServer();

private updateServer doUpdateServer;

private void YourMainForm_Load(object sender, EventArgs e)
{
    UpdateServerTimer = new System.Timers.Timer(msToUpdate);

    // set this to true to run without stopping
    UpdateServerTimer.AutoReset = true;

    UpdateServerTimer.Elapsed += UpdateServerTimerOnElapsed;

    doUpdateServer+= DoUpdateServer;
}

// call this method to start the 'Timer
private void btnStartTheTimer_Click(object sender, EventArgs e)
{
    UpdateServerTimer.Start();
}

private void UpdateServerTimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
{
    // necessary to execute the code indirectly by delegate !
    this.Invoke(doUpdateServer);
}

private void DoUpdateServer()
{
    // do the "work" of updating the server here ...
}
Note that in this example a separate Thread is not used to update: that means the WinForms UI is going to be blocked during the execution of the code in 'DoUpdateServer. My guess is that you will want to use a separate Thread for the update, depending on the design of your application, and how it is to be used at run-time.
 
Share this answer
 
You can have an in memory DataTable to hold all data if you are dealing with gridview. Then modify anything you want to this DataTable. When required you can pass all modified data as a Table type parameter to stored procedure in SQL Server 2008.

Or you can also pass all modified data as an XML type to the stored procedure to update at a time.

Regarding the update operation to happen in every 5 mins you can use timer to call the operation repeatedly on every 5 mins.

Hope this will definitely of help to you.
 
Share this answer
 
Comments
Nguyen Tuan 5-Nov-14 1:23am    
Please can you give me an example?
SRS(The Coder) 5-Nov-14 6:32am    
Below code example added by 'BillWoodruff' can be of help. His answer is describing how to run database update on every 5 mins automatically.Then we need implementation for the method 'DoUpdateServer' where we can do something like mentioned below link how to update bulk data to database using stored procedure.

Please follow the link :- http://www.codeproject.com/Articles/39161/C-and-Table-Value-Parameters
I would recommend you write offline data to MS Acess. Storing data in memory can result in data loss in the case of the pc switching abnormally due to a power cut etc. Storing data in memory can also be a problem for large datasets. In order to update to server every 5 minutes, I recommend you make use of a background worker that is called upon every 5 minutes by the timer. This will keep you application responsive.

I have not provided any code since you said you only wanted ideas.
 
Share this answer
 
Nguyen Tuan - I recommend to use Sql CE or XML file as your local storage and define a separate component to Sync between your local storage and Server storage.

By this you can disconnect you app with the server storage.
 
Share this answer
 
Comments
Nguyen Tuan 9-Nov-14 9:34am    
Hi Liju Sankar,

Your solution has some problem:
- How does not the user allow reading data (Sql CE or XML file)
- How do you sync to Server??

If you have small application like that, you can send to me.
Thanks
[no name] 10-Nov-14 5:35am    
Nguyen Tuan - you can use web service (preferably WCF services) to check with connectivity and Sync with Server. i implemented smart client application s with offline capability using WCF service and XML files as data storage using ADO.NET data adapters in early 2008.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900