Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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);
....
C#
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;
                   }
               }
           }
       }
Posted

1 solution

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
 
Share this answer
 
Comments
[no name] 9-Nov-13 17:54pm    
System.Windows.Forms.Control.Invoke

i am a beginner in asp.net So what is equivalent to this or How can I receive incoming readings all the time using threads and serial port in asp.net
Sergey Alexandrovich Kryukov 9-Nov-13 19:13pm    
I just explained: there is no equivalent. I even tried to explain why such equivalent cannot exist.

If you need an advise for working with a serial port, please explain the problem related to it.
From the very beginning, I don't understand how using a serial port can be useful. The ASP.NET code can work only with the hardware on the server side, not client side. So, what do you have on another end of your serial cable, and why?

—SA
[no name] 10-Nov-13 2:55am    
ok, thanks alot
Sergey Alexandrovich Kryukov 10-Nov-13 11:35am    
You are welcome.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900