我已经读了一些关于编写异步代码,但我仍然有很多问题具体关于我的编程任务。在我的Web表单应用程序中,单击一个按钮时,我想在另一个类中执行一个方法,该方法执行任务(将数据插入到数据库中),同时更新屏幕的进度。例如:c# - 异步代码,更新方法进度到页面
btn1_OnClick(object sender, EventArgs e)
{
//call methodA asynchronously?
}
了methodA包含在另一个类和查询数据库:
public class ClassA
{
public void methodA(param)
{
//insert data into database
//create string that notifies the progress of the method (should be displayed on page)
//insert more data into database
//alter string to re-notify of the progress of the method (to be displayed on page)
}
}
正如你所看到的,我想,以填补这将在的形式显示在页面的字符串标签随着方法的进展。我不知道methodA是否会返回类型“void”,但我仍然把它放在那里。我想这个任务应该异步完成,或者可以使用Web服务。那么我如何以编程的方式来做到这一点?
编辑
我已经看了很多文章,但似乎没有一个有效的描述过程中我的情况。他们都只是描述当长时间运行的作业开始工作时(在这种情况下,methodA)开始或结束时显示消息。我希望方法能够吐出一个字符串(描述它在方法中的位置,即“插入的第一组数据”...),并随着它的进展多次显示它。在我的情况下,这种事情对于webforms来说太过雄心勃勃......如果我要调用一个完成所有工作并收回消息的Web服务,该怎么办?
关于“如何在网页上显示长时间运行的进度”有很多问题......考虑搜索自己,因为它看起来不像任何人都知道好的重复。 – 2014-10-31 05:19:21
@AlexeiLevenkov请阅读我的第一个编辑。 – frontin 2014-10-31 15:41:31
我不知道.... Myaby你应该显示最好的“许多”文章的链接...我得到http://www.bing.com/search?q=c%23%20how %20to%20show%20progress%20of%20long%20running%20operation%20on%20web%20page most say say“start operation,every step should update progress in some persistent state and than poll for the progress via client script on timer”。您可以添加“长轮询”,“websocket”或“singnalR”以搜索更快速反馈的解决方案请求。 – 2014-10-31 15:52:32