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.
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.
Gives me:
Here's my entire routine, including event handler.
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:
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();
}
}
}
