你可以通过给出一些想法或例子来帮助我吗? [英] Can You Help Me By Giving Some Idea Or Example?

查看:70
本文介绍了你可以通过给出一些想法或例子来帮助我吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我使用C#编写Windows Form应用程序。此应用程序脱机运行和写入数据。每5分钟,这个应用程序将数据更新到服务器。



我怎么做??? ??? div class =h2_lin>解决方案

< blockquote>请注意,System.Timers.Timer只是可以在WinForms中使用的几个可能的计时器之一。我建议你阅读这篇CodeProject文章,下载/研究/运行它的代码示例,以查看不同的计时器:所有关于.NET计时器 - 比较[ ^ ]。



此示例使用System.Timers.Timer定期调用函数:

  //  计时器更新间隔(分钟) 
private const int UpdateServerInterval = 5 ;

// 计时器更新间隔(以毫秒为单位)
private Int64 msToUpdate = new TimeSpan( 0 ,UpdateServerInterval, 0 )。毫秒;

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);

// 将此设置为true以便不停止运行
UpdateServerTimer.AutoReset = true ;

UpdateServerTimer.Elapsed + = UpdateServerTimerOnElapsed;

doUpdateServer + = DoUpdateServer;
}

// 调用此方法启动'计时器
private void btnStartTheTimer_Click( object sender,EventArgs e)
{
UpdateServerTimer.Start();
}

private void UpdateServerTimerOnElapsed( object sender,ElapsedEventArgs elapsedEventArgs)
{
// 委托间接执行代码所必需的!
this .Invoke(doUpdateServer);
}

private void DoUpdateServer()
{
// 执行更新服务器的工作......
}

请注意,在此示例中,不使用单独的Thread进行更新:这意味着在DoUpdateServer中执行代码期间将阻止WinForms UI。我的猜测是你需要使用一个单独的Thread进行更新,具体取决于应用程序的设计,以及在运行时如何使用它。


你可以拥有在内存中DataTable,用于在处理gridview时保存所有数据。然后修改您想要此DataTable的任何内容。必要时,您可以将所有已修改的数据作为表类型参数传递给SQL Server 2008中的存储过程。



或者您也可以将所有已修改的数据作为XML类型传递给一次更新的存储过程。



关于每5分钟发生的更新操作,您可以使用定时器每5分钟重复调用一次操作。 br />


希望这对您有帮助。


我建​​议您将离线数据写入MS Acess。将数据存储在存储器中可能导致在由于断电等导致pc异常切换的情况下数据丢失等。将数据存储在存储器中也可能是大数据集的问题。为了每5分钟更新一次服务器,我建议您使用计时器每5分钟调用一次的后台工作程序。这将使您的应用程序响应。



我没有提供任何代码,因为你说你只想要点子。


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???

解决方案

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:

// 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.


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.


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.


这篇关于你可以通过给出一些想法或例子来帮助我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆