Jump to content
The simFlight Network Forums

Help on Program Flow


Recommended Posts

Hi.

I want to create a simple program that can read AIRSPEED (for example) and update it in real time.... lets say every 300 ms. The program im writing is in C#, but the principles are all same everywhere.

My problem is - i wrote the program that does excatly what i said but the program flow is wrong i suspect bcause it takes 16000kb of RAM, and while the program runs i cant press any buttons in it since the sand clock is showing as a cursor. Here is the part of the code:

bool result = false;			// Return boolean for FSUIPC method calls
        int dwFSReq = 0;				// Any version of FS is OK
        int dwOffset = 0x02BC;			// Indicated airspeed memory offset
        int dwSize = 4;					// Indicated airspeed memory size
        int token = 0;					// Variable to hold returned token index
        int dwResult = 0;				// Variable to hold returned results
        int loop = 1;
        int ias = 0;
        public Form1()

        {
            InitializeComponent();
            this.Show();
            Fsuipc fsuipc = new Fsuipc();
            fsuipc.FSUIPC_Initialization();

            result = fsuipc.FSUIPC_Open(dwFSReq, ref dwResult);
            do {
                this.Refresh();
                fsuipc.FSUIPC_Read(dwOffset, dwSize, ref token, ref dwResult);
                fsuipc.FSUIPC_Process(ref dwResult);
                fsuipc.FSUIPC_Get(ref token, ref dwResult);
                ias = dwResult / 128;
                Thread.Sleep(300);
                SUPDATE(ias);

            } while (ias > 40);
            fsuipc.FSUIPC_Close();

        }

public void SUPDATE(int a)
        {

                this.label1.Text = a.ToString();

        }

PLease tell if my program flow is wrong or maybe i should use someother function instead of Thread.Sleep. How should i do it right?

Thanks in advance, if it is easier for u, u can give me suggestions in C++.

P.S. im not very expirienced in programing but i think sufficient enuf. :)

Link to comment
Share on other sites

The program im writing is in C#, but the principles are all same everywhere.

Well, I'm not sure about that. C# is a "managed" language, which as far as I can see means it is interpreted -- hence the large memory usage.

while the program runs i cant press any buttons in it since the sand clock is showing as a cursor.

I don't know C#, but "Sleep" is no good for your program if it is in the one and only thread.

Doesn't a C# Windows program process Windows messages? Surely for a single-threaded program you should be processing messages anyway, and then using the Windows timer (SetTimer, WM_TIMER message or a TIMERPROC routine) to trigger actions at regular intervals.

Please also just open the FSUIPC link once at the start of the program, and close it at the end.

Regards,

Pete

Link to comment
Share on other sites

Hi kenazo,

Public Form1() is the constructor. It's a bit unusual to see this.Show() in the constructor. Rather you should put this in the main program method (public static Main()) after you create your form.

Presumably you have something like

Form1 myForm = new Form1();

after that you should do

myForm.Show();

Alternatively you can do:

Application.Run(myForm);

or

Application.Run(new Form1());

Anyway - to your specific problem:

The application isn't responding because it's in a really tight loop. Thread.Sleep() is still blocking the thread - ie, it's waiting 300ms and then carrying on - it's not allowing other threads to run.

What you should use instead is:

Application.DoEvents();

Which unblocks the thread and allows other windows messages etc to be processed.

But there's a much better way of doing this than creating a continuous loop on the main thread:

On the form designer find the 'Timer' control in the toolbox and drag it onto the form.

It's not a visual component so it appears at the bottom of the form.

This timer allows you to run a method every x milliseconds.

In the properties set the Interval to the number of millisecond - So you want 300.

Then on the events section (click the lightning icon on the top of the properties) double click in the blank space next to 'Tick'.

This will take you to the code and wire up an event handler for you. You will see a new method which will now run every 300ms.

It's probably called:

private void timer1_Tick(object sender, EventArgs e)

Ignore the parameters.

Just write your code in here to get the airspeed back.

At some point in your code need to Enable the timer - by default it won't run.

I suggest enabling it after you have opened FSUIPC because the code in the timer event will crash is it's run before FSUIPC is open.

To achieve this - and some other good practise too - I suggest you setup two more events:

In the form designer click on the (blue) title bar of the form to get the properties/events window to show the properties/events for the form itself.

Then go to the events bit and find:

Load

Again double click in the blank area to the right to make a new event handler. This one will run when the form loads.

In here - do the FSCIPC open stuff and lastly enable the timer.

Timer1.Enabled = true;

(Be sure to move the line

Fsuipc fsuipc = new Fsuipc();

up into the form member variables section so it can be accessed from any method).

Lastly, back to the forms events and make a handler for FormClosing.

Here you can close FSUIPC.

Hope that helps at bit. Everything should run much more smoothly, and with the timer method you won't need to worry about Application.DoEvents() either because the timer is on a different thread anyway.

If you have any questions, just ask.

Paul

Link to comment
Share on other sites

Hi Paul.

Thanks alot for sending me into the right direction. I've done everything as u said and now program does very well. My invistigation into FSUIPC will continue then. BIG THANKS :) . PETE u were right to use timer. Thanks to both of u.

Now. small question to pete. In ur program that comes with SDK "fslook2.exe" when u press autorefresh, which i think updates more then 100 variables every 200 milliseconds or so u managed to keep RAM usage to about 3500kb and CPU usage to about 5% (PentiumIII 3.2GHz RAM 512)

while my program is taking only 1 variable and has 16000kb.... do u somehow delete unneeded variables from fcuipc protocol?

Link to comment
Share on other sites

In ur program that comes with SDK "fslook2.exe" when u press autorefresh, which i think updates more then 100 variables every 200 milliseconds or so u managed to keep RAM usage to about 3500kb and CPU usage to about 5% (PentiumIII 3.2GHz RAM 512)

All my programs are in C (even with some ASM on occasion) which compiles into native Intel Pentium code. It can be very compact and efficient when optimised.

while my program is taking only 1 variable and has 16000kb.... do u somehow delete unneeded variables from fcuipc protocol?

No, it isn't anything to do with the number of variables. I suspect that even if you read the whole 64k address space yours wouldn't expand significantly. C# is not a native Intel program, it is what I call interpreted (Microsoft calls "Managed"). Basically that means that most of the code in the memory occupied is part of the interpreter/manager, not really part of your program. I really doubt you could even write a simple "Hello world" program in C# which didn't take oodles of memory.

This is just my opinion of course. I may well be proved wrong. I've not even moved to C++ because it's too far away from the machine levels I understand better (or used to). I've been a nuts and bolts programmer now for 43 years so I'm entitled to such prejudices! ;-)

Regards,

Pete

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.