Manipulating variable references

Just a quick question,

I have a thread which manages radio communications. It needs to report certain values from the whole program back through the radio for me to keep a tab on whats happening onboard. Is it possible to pass pointers to that object, in order for it to be able to check their states?

E.g. I have bearing position in my imu thread, and position in my gps thread - I would like to be able to monitor those in the radio thread.

How would I go about this?

I would create a class that handles the imu thread + one that handles the gps thread + one that handles the radio thread.

Each class would have public properties, which can be used to reqeust current values.

When getting/setting a property value of a class you wrap a lock (this) {} around it so thread access is synchronised.

That’s indeed my current set-up, and all of it is co-ordinated from a main controller class. I have no problems accessing those variables from the main controller class, but is there a neat way to access them from the radio class?

I know I could just read them using my controller class, and then have a loop feeding them into the radio class, but that seems like a messy way of doing it (It’s the way I’m doing it right now). Thats why I was wondering if it is possible to send just a pointer to the memory location storing that particular variable, so that the radio thread could keep track of it.

The main controller class could buffer the values, and the radio thread could request them from the main controller class, by reading a property or by firing an event that asks for the value(s).

It’s hard to see what you’re doing without seeing any code.

It would be rather difficult to present just a small piece of code, its a part of a large project.

Could you please let me know/suggest a good place to look up how to use events to ask for values? Because that would be rather useful. I haven’t done much event based programming in c# - I’ve not used C# before fez. What I’m trying to avoid is constantly polling the data from the main controller.

A logical event scheme would be:

In your measurement classes you expose an event that gets fired when a measurement changes.

The main controller subscribes to that event.

When the event triggers the handler in the main controller, the main controller updates the value of the radio thread.

F.e. a temperature measurement thread class could contain this:


// function prototype (this is how the handler should look like)
public delegate void TemperatureChangedCallback(object sender, float temperature);

// public event you can subscribe to
public event TemperatureChangedCallback TemperatureChanged;

// Helper function that raises the event when there is/are one or more subscribers
private void OnTemperatureChanged(float temperature)
{
   if (TemperatureChanged != null)
      TemperatureChanged(this, temperature);
}

Every time the temperature changes (compared with a previous value) you fire the event by calling the helper function OnTemperatureChanged

Subscribing to the event:


tempMeasure.TemperatureChanged += new TemperatureChangedCallback(MyTempChangedCallback);

...

private void MyTempChangedCallback(object sender, float temperature)
{
   // pass new temperature to radio thread
}


First note: I wrote the code direclty on the forum and didn’t find the compile button :wink: so bugs can happen…

Second note: that in the full .NET framework you would create a class that derives from EventArgs instead of directly passing “float temperature”… The event itself would then be declared by using the EventHandler<> template class…

Thanks Wouter, this is exactly what I require :).

Time to get coding.

The event method is definitely the best solution, however, what you we’re asking for was probably the [b]out/b or [b]ref/b keywords from C#. Using those keywords, you can construct a method that takes a variable, which the method can manipulate however it wants. This isn’t something I’d recommend, but it answers your original question.

Yes that’s what I was intending to use, but I found that the reference seems to copy a local value to the method, rather than keep track of the original reference. What I was trying to work out is if there is a way of keeping that in reference format. Let me give you a quick code (Not checked)


class something 
{

int trackedValue;

function keepTrackOf(ref int i)
{
trackedValue = i;
}

}


What I was hoping is that the value of trackedValue updates as value of i changes in other threads of the application, after only running that function once. Is that possible?

In C this would be rather easy to do, if one just assigns a pointer to a variable, each time it is evaluated you’d have the variables actual value, however it may have been modified any program, as long as its location remains constant. This is what I’m after here.

So to summarise, I would somehow like to keep a reference to the variable, rather than a local copy of the contents of the variable, in my method. Is that possible?