0

I am currently writing a program in C# that constantly outputs random values between 1 and 12 and displays them in a label control on an ASP.NET web form. While the output is successfully written to the web form, it only displays one value. In other words, I want the label output to track the output (display) each value that is produced by the function as written below to produce a consistently changing value:

[WebMethod]
    public static string StreamData()
        {
            string[] value = new string[1]; 

            Random rnd = new Random();

            Thread t = new Thread(delegate ()
            {
                while (true)
                {
                    value[0] = rnd.Next(1, 13) + "";
                }
            });
            t.Start();


            return value[0]; 

        }

I have tried using an AJAX function to pull the data from the function but I am not too sure if I am going about displaying the data in the right way:

     <script type="text/javascript">
    $(function () {
        $.ajax({
            type: 'POST',
            url: 'default.aspx/streamdata',
            data: '',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (msg) {
                //alert(msg.d);
                $('#<%= lblReading.ClientID %>').html(msg.d);
            }
        });
             });
</script>

The program is being written to test the concept of trending live data from PLCs using TIA Portal 15 with said data obtained from sensor readings across a production line. The idea behind this program is to serve as a proof of concept that real-time data can in fact be displayed and trended in an ASP.NET web environment to be accessed across a range of web-compliant devices. Despite my efforts in displaying random values in this way, my attempts have been thus far unsuccessful, although I am determined to not give up. I researched this method which uses SignalR to achieve something similar to my desired solution, though I am unsure of how to implement it in my case: How to implement real time data for a web page

Could somebody please shed some light on how the above might be achieved?

Many Thanks,

Ben.

BStanway
  • 7
  • 6
  • 2
    Have you met [SignalR](http://signalr.net/)? – Zohar Peled Sep 03 '18 at 11:34
  • First step in your AJAX would be appending your result to your label instead of overwriting it. – AsheraH Sep 03 '18 at 11:55
  • But the idea is to display random changing values that can be trended. Temporary storage and display of values such as is proposed by a method of appendage is not what I am looking for. Think of the display as say a tachometer which displays speed output values, for example. – BStanway Sep 03 '18 at 12:35

1 Answers1

-1

Your requirement can be accomplished in ASP.Net web forms using SignalR. Please take a look at this small tutorial on how to do that, Adding SignalR to an ASP.NET WebForms Project

Nouman
  • 591
  • 6
  • 14
  • It is frowned upon to answer a question with a link to a tutorial. Please read https://stackoverflow.com/help/how-to-answer to get more information on how to format a good answer. – JanWillem Huising Sep 03 '18 at 11:58