• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

Getting eventhandler error, but only under certain conditions...

Messages
108
Country
us-newmexico
Here's what does work:
If I loop my call to method invoking SimConnect.receive routine, that works fine. I run a 1-second loop and use the .ONCE option for SIMCONNECT_PERIOD. This works fine for both F5 and CTL F5.

C#:
while (true)
            {
                DataManager.GetData(oSimConnect);
                Thread.Sleep(1000);
            }
.
.
.
 public static void GetData(SimConnect oSimConnect)
        {
            oSimConnect.ReceiveMessage();
            oSimConnect.RequestDataOnSimObject(DATA_REQUESTS.REQUEST_ONE, DATA_DEFINITIONS.sWeatherValues, SimConnect.SIMCONNECT_OBJECT_ID_USER,SIMCONNECT_PERIOD.SECOND,0,0,1,0);
            Console.WriteLine("We did the request...");
           //Console.ReadKey();                        
         }

However, if I take away the loop, I get event handler error if I CTL-F5. (I run the compiled version so the app doesn't close and I don't have to put in readkey silliness to keep window open.,.. and to ensure processes are actually running.) This happens for any setting of SIMCONNECT_PERIOD, even ONCE.
C#:
//while (true)
            //{
                DataManager.GetData(oSimConnect);
               //Thread.Sleep(1000);
            //}

Gives me:
1662503365705.png


Here's my entire routine, including event handler.


C#:
namespace SimConnector
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.FlightSimulator.SimConnect;
using System.Runtime.InteropServices;
using System.Timers;
using System.Threading;

    
//Master Class
/*---------------------------------*/
    class Program
    {
        const uint WM_USER_SIMCONNECT = 0x0402;
        static void Main(string[] args)
        {
            //SimConnect object.  We'll need to pass this around.           
            SimConnect oSimConnect;
            oSimConnect = ConnectionManager.OpenSimConnect(WM_USER_SIMCONNECT);
            if (oSimConnect != null)
            {
                Console.WriteLine("SimConnect established");
            }
            else
            {
                Console.WriteLine("SimConnect failed");
                Console.ReadKey();
                Environment.Exit(0);
            }
            /*Console.ReadKey();
            Console.WriteLine(ConnectionManager.CloseSimConnect(oSimConnect));
            Console.ReadKey();*/
            /**/
            //Let's try to do some data work.
            Console.WriteLine("Trying to do some data work, press any key to continue.");
            Console.ReadKey();
            DataManager.CreateDataDefinition(oSimConnect);
            Console.WriteLine("Trying to register... press a key if you dare.");
            Console.ReadKey();
            DataManager.RegisterDefinitions(oSimConnect);
            Console.WriteLine("Now we will actually get data... maybe");
            Console.ReadKey();
            //while (true)
            //{
                DataManager.GetData(oSimConnect);
               //Thread.Sleep(1000);
            //}
        }           
    }
    /*=========================================
    ---------------------------------------------
    UTILITY CLASSES
    ---------------------------------------------
    ===========================================*/
    
    /*========================================
     CONNECTIION MANAGER
     ========================================*/
    class ConnectionManager
    {
        /*CREATE SIMCONNECT OBJECT*/
        //-------------------------
        public static SimConnect OpenSimConnect(uint idConnect)
        {
            SimConnect oSimConnect;           
            {
                try
                {
                    oSimConnect = null;
                    oSimConnect = new SimConnect("SimConnector", IntPtr.Zero, idConnect, null, 0);                 
                }
                catch (COMException ex)
                {
                    return null;
                }
                return oSimConnect;
            }           
        }
        /*-----------------------
        =========================
        ------------------------*/
        /*DESTROY SIMCONNECT OBJECT*/
        public static int CloseSimConnect(SimConnect oSimConnect)
        {
            if(oSimConnect !=null)
            {
                oSimConnect.Dispose();
                oSimConnect = null;
                return 0;
            }
            else
            {
                return 1; // SimConnect object does not exist.
            }
        }
        /*-----------------------
        ========================
        ------------------------*/                 
    }/*CREATE STRUCTS FOR DATA REQUESTS*/
    //Define the struct layout.
    [StructLayout(LayoutKind.Sequential,CharSet =CharSet.Ansi,Pack =1)]
    struct sWeatherValues
    {
        public double AmbientTemperature;
    }
    //Define the Request enum
    enum DATA_REQUESTS
    {
        REQUEST_ONE,
    }
    enum DATA_DEFINITIONS
    {
        sWeatherValues,
    }
    //Data Definitions
    class DataManager
    {
        public static void CreateDataDefinition(SimConnect oSimConnect)
        {
            if (oSimConnect != null)
            {
                oSimConnect.AddToDataDefinition(DATA_DEFINITIONS.sWeatherValues, "AMBIENT TEMPERATURE", "CELSIUS", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
                Console.WriteLine("I guess we added a data definition");
                Console.ReadKey();
                
            }
            else
            {
                Console.WriteLine("FSX isn't running.");
                Environment.Exit(0);
            }


            
        }
        public static void RegisterDefinitions(SimConnect oSimConnect)
        {
            if (oSimConnect != null)
            {
                oSimConnect.RegisterDataDefineStruct<sWeatherValues>(DATA_DEFINITIONS.sWeatherValues);
                Console.WriteLine("I guess we registered a data definitinon");
                Console.ReadKey();
                Console.WriteLine("And now we will subscribe to the event handler");
                oSimConnect.OnRecvSimobjectData += new SimConnect.RecvSimobjectDataEventHandler(SimConnect_OnRecvSimobjectData);
                Console.WriteLine("Subscribed");               
            }
            else
            {
                Console.WriteLine("FSX isn't running.");
                Environment.Exit(0);
            }       
        }
        public static void GetData(SimConnect oSimConnect)
        {       
        
            oSimConnect.ReceiveMessage();
            oSimConnect.RequestDataOnSimObject(DATA_REQUESTS.REQUEST_ONE, DATA_DEFINITIONS.sWeatherValues, SimConnect.SIMCONNECT_OBJECT_ID_USER,SIMCONNECT_PERIOD.ONCE,0,0,1,0);
            Console.WriteLine("We did the request...");
           //Console.ReadKey();           
                
        
        }
        private static void SimConnect_OnRecvSimobjectData(SimConnect sender, SIMCONNECT_RECV_SIMOBJECT_DATA data)           
        {
            Console.Write("data.dwData is ");
            Console.WriteLine(data.dwRequestID);
            Console.Write("DATA_REQUESTS.REQUEST_ONE ");
            Console.Write(DATA_REQUESTS.REQUEST_ONE + " ");
            Console.WriteLine(DATA_REQUESTS.REQUEST_ONE == 0);
            Console.WriteLine(data.dwData[0]);
            sWeatherValues s1 = (sWeatherValues)data.dwData[0];
            Console.WriteLine(s1.AmbientTemperature);               
            //Console.ReadKey();           
        }
    }
}
 

Attachments

  • 1662503182596.png
    1662503182596.png
    25 KB · Views: 168
Back
Top