Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从服务器端线程更新面板_C#_Asp.net_Ajax_Multithreading_Updatepanel - Fatal编程技术网

C# 从服务器端线程更新面板

C# 从服务器端线程更新面板,c#,asp.net,ajax,multithreading,updatepanel,C#,Asp.net,Ajax,Multithreading,Updatepanel,我正试图开发一个聊天使用aspx。我已经在使用asmx+winform时得到了它,但是现在我在使用asmx+aspx时遇到了问题 基本上,我想做的是每两秒钟调用WebMethodCheckFormMessages,如果有消息,将它们添加到更新面板内的列表框中,然后执行UpdatePanel1.update() 问题是,显然我不能像在winform中那样使用线程 void check() { while (true) { Thread.Sleep(2000); string m

我正试图开发一个聊天使用aspx。我已经在使用asmx+winform时得到了它,但是现在我在使用asmx+aspx时遇到了问题

基本上,我想做的是每两秒钟调用WebMethod
CheckFormMessages
,如果有消息,将它们添加到更新面板内的列表框中,然后执行
UpdatePanel1.update()

问题是,显然我不能像在winform中那样使用线程

void check() {
  while (true) {
    Thread.Sleep(2000);
    string message = CheckForMessages();
    if (message != "") {
      ListBox1.Items.Add(message);
      UpdatePanel1.Update();
    }
  }
}
我这样开始线程:

protected void Page_Load(object sender, EventArgs e) {
  timer = new Thread(new ThreadStart(check));
  timer.Start();
}
它不会抛出异常或任何东西,程序按预期运行。调用web服务,如果有消息,则返回字符串,线程将消息添加到列表中并调用
UpdatePanel1.Update()。然而,专家组没有更新


问题可能是什么?

请尝试相反的方法:让客户端主动查询服务器,并相应地更新其状态,而不是让服务器主动将更改推送到客户端

一种方法是创建一个按钮来手动查询服务器。在验证它是否有效后,使用css隐藏该按钮,并设置一个函数以定期单击该按钮。您可以使用javascript
setInterval
进行此操作

要在javascript中单击按钮,可以执行以下操作:

setInterval(function(){ document.getElmtById(“<%= theButton.ClientID %>").click(); }, 2000);
setInterval(function(){document.getElmtById(“”。click();},2000);

上述代码应每隔2秒单击按钮。

在ASP.Net中,您可以使用计时器控件和updatepanel

<asp:UpdatePanel runat="server" ID="uxUpdatePanel" UpdateMode="Conditional" EnableViewState="true">
        <ContentTemplate>
            <div>
                <asp:Label ID="Label1" runat="server" style="display:none;"><%=GetMessageLog()%></asp:Label> 
                <asp:ListBox runat="server" ID="ListBox1" EnableViewState="true">

                </asp:ListBox>
            </div>           
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="uxTimer" EventName="Tick" />
        </Triggers>
    </asp:UpdatePanel>
    <asp:Timer runat="server" ID="uxTimer" Interval="2000">
    </asp:Timer>

这应该适合您。但我建议您使用Javascript和JQuery,使用setTimeout函数异步调用Web服务。

您应该看看SignalR.net。Jabbr.net基于SignalR,正是您所需要的。

是的,Vishal…可能不太复杂:

<asp:UpdatePanel ID="updatePanelPromptB" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="labelPlayback" runat="server"></asp:Label>
</ContentTemplate> 
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timerPromptB" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer runat="server" ID="timerPromptB" OnTick="timerPromptB_Tick" Enabled="false" Interval="4000" />

private void promptBottom(string text)
{
    labelPlayback.Text = text;
    updatePanelPromptB.Update();
    timerPromptB.Enabled = true;
}
protected void timerPromptB_Tick(object sender, EventArgs e)
{
    labelPlayback.Text = "OK";
    timerPromptB.Enabled = false;
}

私有void promptBottom(字符串文本)
{
labelPlayback.Text=文本;
updatePanelPromptB.Update();
timerPromptB.Enabled=true;
}
受保护的void timerPromptB_Tick(对象发送方,事件参数e)
{
labelPlayback.Text=“确定”;
timerPromptB.Enabled=false;
}

edit:我高估了我的javascript技能。请提供一个代码示例,以便每x秒单击一个按钮?
<asp:UpdatePanel ID="updatePanelPromptB" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="labelPlayback" runat="server"></asp:Label>
</ContentTemplate> 
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timerPromptB" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer runat="server" ID="timerPromptB" OnTick="timerPromptB_Tick" Enabled="false" Interval="4000" />

private void promptBottom(string text)
{
    labelPlayback.Text = text;
    updatePanelPromptB.Update();
    timerPromptB.Enabled = true;
}
protected void timerPromptB_Tick(object sender, EventArgs e)
{
    labelPlayback.Text = "OK";
    timerPromptB.Enabled = false;
}