简体   繁体   English

如何在客户端显示ac#.NET Web应用程序中服务器端处理的状态

[英]How to display on the client side the status of server side processing in a c# .NET web application

I have this c# .net web application which (amongst other things) parses all text files in a folder on the server and inserts info from these files in a database; 我有这个c#.net Web应用程序,它(除其他外)解析服务器上某个文件夹中的所有文本文件,并将这些文件中的信息插入数据库中; this happens in a Button_Click method and takes quite a long time (the text files are pretty big). 这种情况发生在Button_Click方法中,并且需要相当长的时间(文本文件很大)。 How can i (if at all possible) display which file the server is currently parsing in a label on the calling web page ? 我如何(如果有可能)在调用网页的标签中显示服务器当前正在解析的文件?

here is the relevant aspx code : 这是相关的aspx代码:

<asp:Label ID="Label2" runat="server"></asp:Label>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Parse" />

and here the code behind c# code : 这是c#代码背后的代码:

protected void Button1_Click(object sender, EventArgs e)
{
    string path = @"c:\myPath";       
    var listOfFiles = Directory.EnumerateFiles(path);
    foreach (string currentFileName in listOfFiles)
    {
        Label1.Text = "Status : Parsing file " + currentFileName;

        // some text files processing code
        // some database code

    }
}

Of course, this doesn't work as intended, the label only displays the name of the last processed file. 当然,这不能按预期工作,标签仅显示最后处理的文件的名称。 I understand why this is so (and only wrote it in my code at first because i was stupid and not paying attention), but i'd like to overcome this problem (if at all possible). 我理解为什么会这样(并且起初只是在我的代码中编写它是因为我很愚蠢并且没有引起注意),但是我想克服这个问题(如果可能的话)。 Thanks in advance 提前致谢

I would, like bsayegh, recommend SignalR. 我会像bsayegh一样,推荐SignalR。 On the server side, you can code up an async process like 在服务器端,您可以编写一个异步过程,例如

public void  WatchCurrentFile() 
{
  // start working in a new thread
  var task = Task.Factory.StartNew(() => GetFile());

  task.ContinueWith(t =>
    { Caller.updateFile(t.Result); });
}

private string GetFile()
{
  string currentFile = "";
  return currentFile;
  // Alternately update asp the label here.
}

then on the client side, have a script like 然后在客户端,有一个像

<script type="text/javascript">
  // init hub 
  var proxy = $.connection.signals;

  // declare response handler functions, the server calls these
  proxy.updateFile = function (result) {
    alert("Current file being processed: " + result);
  };

  // start the connection
  $.connection.hub.start();

  // Function for the client to call
  function WatchCurrentFile() {
    proxy.WatchCurrentFile(); 
  }
</script>

The SignalR task factory will asynchronously call updateFile(), which in turn periodically calls GetFile() to update your client. SignalR任务工厂将异步调用updateFile(),而后者又会定期调用GetFile()来更新您的客户端。

foreach (string currentFileName in listOfFiles)
{
    Label1.Text = "Status : Parsing file " + currentFileName;
    Thread.Sleep(1000);
    // some text files processing code
    // some database code

}

First you have to add using System.Threading; 首先,您必须using System.Threading;添加using System.Threading; namespace. 命名空间。 by this code every file show for 1 second. 通过此代码,每个文件显示1秒钟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM