如何从其他线程更新 ASP.NET 中文本框中的值?

发布于 2024-10-21 00:12:39 字数 799 浏览 2 评论 0原文

我在 IIS7.5 和 WCF 回调技术上使用 ASP.NET 4.0。我的回调没有问题。 wcf 服务可以在 Web 客户端中触发回调方法,但它似乎与 UI 线程位于另一个线程上。

public partial class _Default : System.Web.UI.Page, IServiceCallback
{
    private IService proxy = null;
    private static TextBox _textBoxtest;

    protected void Page_Load(object sender, EventArgs e)
    {
        _textBoxtest = TextBox1;
    }      

    protected void Button1_Click(object sender, EventArgs e)
    {
        //then server will call back to FireCallBackFromServer
        proxy.CallService(type, "someObject");
    }

    #region IServiceCallback Members

    public void FireCallBackFromServer(string txt)
    {
        TextBox1.Text = txt; <-- the value does not update on textBox
    }

    #endregion
}

请帮助我思考如何从回调事件更新我的文本框。

谢谢。

I'm using ASP.NET 4.0 on IIS7.5 and WCF Callback technique. I have no problem with callback. The wcf service can fire callback method in web client but it seems it's on another thread with the UI thread.

public partial class _Default : System.Web.UI.Page, IServiceCallback
{
    private IService proxy = null;
    private static TextBox _textBoxtest;

    protected void Page_Load(object sender, EventArgs e)
    {
        _textBoxtest = TextBox1;
    }      

    protected void Button1_Click(object sender, EventArgs e)
    {
        //then server will call back to FireCallBackFromServer
        proxy.CallService(type, "someObject");
    }

    #region IServiceCallback Members

    public void FireCallBackFromServer(string txt)
    {
        TextBox1.Text = txt; <-- the value does not update on textBox
    }

    #endregion
}

Please help me to think how to update my textBox from callback event.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夕嗳→ 2024-10-28 00:12:39

这就是 WCF 回调的工作原理。每个回调调用都由其自己的线程提供服务。我认为发生这种情况的原因是因为您没有 SynchronizationContext 它将传入请求指向当前线程(希望是页面的当前实例)。相反的示例是 WPF 或 WinForm 应用程序中使用的回调。默认情况下,这些应用程序中的 UI 线程具有 SynchronizationContext,因此如果您在 UI 线程中打开服务代理,回调请求将被路由回 UI 线程 - 有时会导致另一个问题,因此您可以关闭 的使用ServiceBehaviorAttribute 中的 >SynchronizationContext

但即使您解决了这个问题,您在 ASP.NET 中也会遇到同样的问题。对 ASP.NET 的每个请求都会创建新的处理程序实例。因此,来自浏览器的每个请求都会创建新的页面实例。

我相信,如果客户端是 ASP.NET,那么 WCF 回调就没有意义,因为我仍然没有看到任何有效的实现。

It is how WCF callback works. Each callback call is served by its own thread. I think the reason why this happens is because you don't have SynchronizationContext which will point incomming request back to current thread (and hopefully current instance of your page). The contrary example are callbacks used in WPF or WinForm applications. UI thread in these applications by default has SynchronizationContext so if you open service proxy in UI thread, requests to callback are routed back to UI thread - it sometimes causes another problems so you can turn off usage of SynchronizationContext in ServiceBehaviorAttribute.

But even if you solve this problem you will deal with the same problem in ASP.NET. Each request to ASP.NET creates new instance of handler. So each request from your browser will create new instance of page.

I believe that if client is ASP.NET then WCF callback doesn't make sense because I still didn't see any working implementation.

ζ澈沫 2024-10-28 00:12:39

我遇到过这个问题,在使用 WCF 回调的 WPF 应用程序中,只有 UI 线程可以执行 UI 更新。我在 ASP.NET 中没有做太多工作,所以我不能 100% 确定答案是相同的,但问题看起来非常相似。

我解决这个问题的方法是使用 Dispatcher 和 lambda 将更改发送到 UI 线程。放入代码的上下文中,它看起来像这样,

public void FireCallBackFromServer(string txt)
{
    Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => TextBox1.Text = txt;));
}

这应该将文本框的内容更新为回调中提供的文本。尝试一下,看看效果如何。

I've run into this issue, where only the UI thread can perform UI updates, in a WPF application using WCF callbacks. I don't do much work in ASP.NET, so I'm not 100% sure the answer is the same but the problem looks very similar.

The way I solved the problem was to use the Dispatcher and lambdas to send the change to the UI thread. Put into the context of your code, it would look something like

public void FireCallBackFromServer(string txt)
{
    Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => TextBox1.Text = txt;));
}

This should update your textbox's content to the text provided in the callback. Give it a try and see how you go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文