什么在asp.net中等效于c#中的this.invoke() [英] what is equivalent in asp.net to this.invoke() in c#

查看:75
本文介绍了什么在asp.net中等效于c#中的this.invoke()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将以下用C#编写的代码转换为Asp.net

这里,一段C#代码



private delegate void DelegateAddToList(string msg);

private DelegateAddToList m_DelegateAddToList;



....

m_DelegateAddToList = new DelegateAddToList(AddToList);



void AddToList(){...}

.....



线程t =新线程(ReceiveThread);

....

I want to convert the following code written in C# to be in Asp.net
Here, a piece of C# code

private delegate void DelegateAddToList(string msg);
private DelegateAddToList m_DelegateAddToList;

....
m_DelegateAddToList = new DelegateAddToList(AddToList);

void AddToList(){...}
.....

Thread t = new Thread(ReceiveThread);
....

private void ReceiveThread()
       {
           while (true)
           {
               runThread.WaitOne(Timeout.Infinite);
               while (true)
               {
                   try
                   {
                       // receive data
                       string msg = serialPort.ReadLine();
                       this.Invoke(this.m_DelegateAddToList, new Object[] { "R: " + msg });
                   }
                   catch
                   {
                       try
                       {

                           this.Invoke(this.m_DelegateStop, new Object[] { });
                       }
                       catch { }
                       runThread.Reset();
                       break;
                   }
               }
           }
       }

推荐答案

这不是这个。调用。这是 System.Windows.Forms.Control.Invoke System.Windows.Threading.Dispatcher

http://msdn.microsoft.com/en- us / library / zyzhdc6b(v = vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/a1hetckb(v = vs.110).aspx [ ^ ],

http://msdn.microsoft.com /en-us/library/system.windows.threading.dispatcher%28v=vs.110%29.aspx [ ^ ]。



虽然 System.Windows.Forms.Control.Invoke 仅用于 System.Windows.Forms System.Windows.Threading.Dispatcher 可用于表单和WPF。



不,ASP.NET没有它的类比,原因很简单:ASP应用程序本身不类似于Forms或WPF应用程序,它的生命周期完全不同。 ASP.NET应用程序实际上不是面向公平的UI,而是客户端的页面。也就是说,所有事件都是通过JavaScipt在客户端处理的(尽管ASP.NET技术会产生一些错觉,即代码隐藏和浏览器内的UI紧密集成,通过回发,但实际上,理解这一点非常重要这只是一种幻觉,或者,如果你愿意的话,这是一个比喻)。一切都归结为HTTP是无状态协议,主要支持客户端 - 服务器模型

http://en.wikipedia.org/wiki/HTTP#HTTP_session_state [ ^ ],

http://en.wikipedia.org/wiki/Stateless_protocol [ ^ ],

http://en.wikipedia.org/wiki/Client -server_model [ ^ ]。



从代码隐藏的角度来看,页面的所有生命周期都在HTTP请求和HTTP响应之间; ASP.NET代码所做的就是创建HTTP响应,然后将其传递到客户端,并在客户端展开所有UI。回调允许在事件处理中涉及代码,但开发人员应该理解它严重影响性能(至少在这些日子里)并且应该不经常发生。



所有这些都使桌面UI风格的线程和调用变得无关紧要。不是说ASP.NET中的线程是不可能的。这是可能的,但主要是无用的。如果您分析上述注意事项,您将理解它。但是,任务并行仍然可以有效地用于某些事情(在多CPU /多代码系统上) ),但这是一个与UI无关的完全不同的故事。



-SA
This is not "this.Invoke". This is either System.Windows.Forms.Control.Invoke or System.Windows.Threading.Dispatcher:
http://msdn.microsoft.com/en-us/library/zyzhdc6b(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/a1hetckb(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher%28v=vs.110%29.aspx[^].

While System.Windows.Forms.Control.Invoke is used only for System.Windows.Forms, System.Windows.Threading.Dispatcher can be used for both Forms and WPF.

And no, ASP.NET does not have analog of it, by one simple reason: an ASP application itself is not analogous to a Forms or a WPF application, its life cycle is totally different. ASP.NET application is not really an even-oriented UI, but a page in the client side is. That said, all the events are really handled on client side via JavaScipt (even though the ASP.NET technology creates some illusion that code behind and in-browser UI is tightly integrated, through postbacks, but in reality, it's very important to understand that this is only an illusion, or, if you will, a metaphor). Everything is reduced to the fact that HTTP is a stateless protocol majorly supporting client-server model:
http://en.wikipedia.org/wiki/HTTP#HTTP_session_state[^],
http://en.wikipedia.org/wiki/Stateless_protocol[^],
http://en.wikipedia.org/wiki/Client-server_model[^].

From the code-behind perspective, all the lifetime of the page lies between HTTP request and HTTP response; all that the ASP.NET code does is creation of the HTTP response which is then delivered to the client side and all the UI is unfolded on the client side. The callback allows to involve code behind in event handling, but a developer should understand that it seriously compromises performance (at least these days) and should happen not too often.

All that makes desktop UI-style threading and invocation quite irrelevant. Not that threading in ASP.NET is impossible. It is possible, but majorly useless. You will understand it if you analyze the above considerations. However, Tasks and Parallel can still be effectively used for certain things (on a multi-CPU/multi-code system), but this is a whole different story not really related to UI.

—SA


这篇关于什么在asp.net中等效于c#中的this.invoke()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆