Ciao ho "risolto" il mio problema in questo modo:
Ho creato un updatepanel con due pulsanti asincroni, uno che fa partire il lavoro
e quindi il thread contemporaneamente esegue una funzione javascript che ogni secondo cliccka l'altro tasto asincrono che aggiorna la label e controlla che il lavoro sia terminato.
Codice .net
codice:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="Test.TestPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
function PremiTasto() {
setInterval(function () { document.getElementById('<%= Button2.ClientID %>').click(); }, 1000);
}
</script>
<style type="text/css">
.hidden {
visibility: hidden;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="StartThread" OnClientClick="PremiTasto();" OnClick="Button1_Click" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:Button ID="Button2" CssClass="hidden" runat="server" Text="Aggiorna" OnClick="Button2_Click" EnableTheming="True" />
<hr />
<asp:Label runat="server" ID="Progress"></asp:Label>
<br />
<asp:Image runat="server" id="LoaderGif" Width="10%" Height="10%"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />
<asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Codice c#
codice:
using System;
using System.Threading;
namespace Test
{
public partial class TestPage : System.Web.UI.Page
{
static int n = 0;
static Thread Test1;
public void Button1_Click(object sender, EventArgs e)
{
Progress.Text = "..:: Caricamento Dati ::..";
LoaderGif.ImageUrl = "https://d13yacurqjgara.cloudfront.net/users/12755/screenshots/1037374/hex-loader2.gif";
Test1 = new Thread(ContaFinoMille);
Test1.Start();
}
public void ContaFinoMille()
{
Label1.Text = "Numero = " + n.ToString();
for (int a = 0; a <= 300; a++)
{
n = a;
Thread.Sleep(100);
}
Test1.Abort();
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text = n.ToString();
if (!Test1.IsAlive)
{
Progress.Text = "..:: Fine Caricamento ::..";
LoaderGif.ImageUrl = "";
}
}
}
}