用于aspx页面等待指示符的多线程C#

时间:2013-10-03 15:46:08

标签: c# asp.net multithreading

我试图在我的aspx页面上指出它正在处理请求。也许这不是最好的方法。但我似乎无法找到另一种方法来实现这一目标。我希望我想要做的事情是有道理的。我感谢任何帮助和建议。

无论如何这里是ASPX代码:

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="content">

<style type="text/css">
    #UpdatePanel1, #UpdatePanel2, #UpdateProgress1 { 
        border-right: gray 1px solid; border-top: gray 1px solid; 
        border-left: gray 1px solid; border-bottom: gray 1px solid;
    }
    #UpdatePanel1, #UpdatePanel2 { 
        width:200px; height:200px; position: relative;
        float: left; margin-left: 10px; margin-top: 10px;
    }
    #UpdateProgress1 {
        width: 400px; background-color: #FFC080; 
        bottom: 0%; left: 0px; position: absolute;
    }
</style>
    <div>
        <asp:ScriptManager ID="ScriptManager2" runat="server" />
        <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
            <ContentTemplate>
                <%=DateTime.Now.ToString(CultureInfo.InvariantCulture) %> <br /> 
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:UpdateProgress ID="UpdateProgress1" runat="server">
            <ProgressTemplate>
                Archiving...
            </ProgressTemplate>
        </asp:UpdateProgress>
    </div>

<table cellpadding="0" cellspacing="0">
    <tr>
        <td>
            <ul>
                <li>
                    <span>
                        <asp:ImageButton ID="ArchiveImageButton" runat="server" AlternateText="Archive Compliance"
                            ImageUrl="~/images/Icons/24/452-bank.gif" OnClick="Button_Click" ToolTip="Archive compliance results" />
                    </span>

                </li>
            </ul>
        </td>
    </tr>

这是C#Code-Behind:

    private bool archiving;

    protected void Button_Click(object sender, EventArgs e)
    {
        archiving = true;
        WaitHandle wait = new EventWaitHandle(false, EventResetMode.AutoReset);
        ThreadPool.QueueUserWorkItem(state => btnArchive_Click(sender, e));
        ThreadPool.QueueUserWorkItem(sleepState => Sleep());
        ThreadPool.RegisterWaitForSingleObject(wait, delegate { WakeMeUp(); }, false, 9999999999999999, true);
    }

    private void WakeMeUp()
    {
        archiving = false;
    }

    private void Sleep()
    {
        while (archiving)
        {
            Thread.Sleep(1);
        }
    }

    protected void btnArchive_Click(object sender, EventArgs e)
    {
         //DO WORK
         ShowSuccessMessage("Compliance results have been archived.");
    }

1 个答案:

答案 0 :(得分:1)

出于您的目的,听起来使用JavaScript显示叠加层会更容易。代码如下,here是一个jsFiddle来演示。

<script type="text/javascript">
    showOverlay = function(){
        var overlay = document.getElementById("overlay");
        if (overlay){
            overlay.style.display = "block";
        }
    }     
</script>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick="showOverlay();" />
<div id="overlay">
    <div id="wait-dialog">Please wait while the form processes...</div>
</div>

这是你需要的CSS:

#overlay {
    display: none;
    position: absolute;
    left: 0;
    top: 0;
    background-color: #666;
    width: 100%;
    height: 100%;
    z-index: 100;
}
div#wait-dialog {
    margin: 0px auto;
    padding: 20px;
    text-align:center;
    width: 400px;
    min-height: 200px;
    background-color: #ccc;
}