locked
Plotting a Chart with data obtained from a different thread! RRS feed

  • Question

  • User-1540960300 posted

    Hi there,

    I'm tying to plot some data which I get in the client_MqttMsgPublishReceived event from the M2Mqtt Library. I have programed this first for a desktop application and the threading problem is solved by using the Dispatcher.Invoke as shown in the code below.

    void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
    {
    string ReceivedMessage = Encoding.UTF8.GetString(e.Message);
    
    
    Dispatcher.Invoke(delegate
    { // we need this construction because the receiving code in the library and the UI with textbox run on different threads
    
         ...
    
    });

    I know I can not use the same construction in ASP.NET but after reading a lot of posts related to Multi-Threading and other related to I have not been able to solve my problem.

    When I try to acces the chart from the received data event I receive a NullObjectReference. In other case when I try to put the data in some global variable, this data is not accesible from the UI controller.

    What I want to do is to refresh the Chart data from the received data event or insert the received data into some global variables to then plot in the Chart. 

    Do someone know a way to solve this?

    Thanks in advance

    Tuesday, November 19, 2019 3:31 PM

All replies

  • User475983607 posted

    Do someone know a way to solve this?

    Your original solution is related to handling system events behind the UI thread with a single user where state is always maintained.  Web applications have multiple users in a stateless environment where making an HTTP request (clicking a button or link) causes code to instantiate and execute.  This code is torn down at the end of the request unlike a Desktop app where the code continues to run until the application is closed.

    As far as I can tell M2MQTT is a real-time system.  You'll need a service that is always running to handle the subscriptions or you'll need a way for the client to get HTTP data on each request.  SignalR or ASP.NET Core hosted services could be a solution.  Most likely you need M2MQTT support.

    Tuesday, November 19, 2019 4:25 PM
  • User-1540960300 posted

    Thank you so much for your help!

    Wednesday, November 20, 2019 2:39 PM