Waiting for variable to change

A

Anders Eriksson

I'm programming in C# and .NET 4.0.30319 SP1Rel in VS2010

Short version:
I have a dll sequence of functions

void MainFunction()
{
RotateDevice()
MoveDeviceX()
MoveDeviceZ()
}

The first function (RotateDevice) will set a variable
(startRotation=true) and must wait until the variable reachedPosition is
set to true. Then the next function will be called (MoveDeviceX)

I'm struggling with this! So if someone can help I would appreciate it!

Longer version:
As I said I have an dll, that communicates with some hardware using COM.
A new request has come and I need to add the function RotateDevice
(which rotates the device around the X axis) This is done using two
variables. A double containing an angle and a bool(startRotation) to
start the rotation. The COM method is asynch so it will return directly
and when it has reached the position another bool(reachedPosition) will
be set to true.

Since the device can't move in multiply direction simultaneously I must
wait for the movement to finish (Strangely enough the movement in X and
Z is handled differently so I don't have this problem here)

How would I make the function RotateDevice wait for the reachedPosition
to become true?

Hopefully this is understandable!

// Anders
 
M

Marcel Mueller

As I said I have an dll, that communicates with some hardware using COM.
A new request has come and I need to add the function RotateDevice
(which rotates the device around the X axis) This is done using two
variables. A double containing an angle and a bool(startRotation) to
start the rotation. The COM method is asynch so it will return directly
and when it has reached the position another bool(reachedPosition) will
be set to true.

If this is the only (awful) API, you are stuck with polling. I.e.
waiting in an infinite loop until your variable is true or you don't
want to wait any longer.

How would I make the function RotateDevice wait for the reachedPosition
to become true?

while (!Volatile.Read(ref reachedPosition))
Thread.Sleep(1);

This pauses for one time slice after each check - at least with Windows
kernel - and therefore does not hog the CPU.


Marcel
 
A

Arne Vajhøj

I'm programming in C# and .NET 4.0.30319 SP1Rel in VS2010

Short version:
I have a dll sequence of functions

void MainFunction()
{
RotateDevice()
MoveDeviceX()
MoveDeviceZ()
}

The first function (RotateDevice) will set a variable
(startRotation=true) and must wait until the variable reachedPosition is
set to true. Then the next function will be called (MoveDeviceX)

I'm struggling with this! So if someone can help I would appreciate it!

Longer version:
As I said I have an dll, that communicates with some hardware using COM.
A new request has come and I need to add the function RotateDevice
(which rotates the device around the X axis) This is done using two
variables. A double containing an angle and a bool(startRotation) to
start the rotation. The COM method is asynch so it will return directly
and when it has reached the position another bool(reachedPosition) will
be set to true.

Since the device can't move in multiply direction simultaneously I must
wait for the movement to finish (Strangely enough the movement in X and
Z is handled differently so I don't have this problem here)

How would I make the function RotateDevice wait for the reachedPosition
to become true?

The simple solution is to poll the variable:

while(!reachedPosition) Thread.Sleep(POLL_INTERVAL);

reachedPosition better be declared volatile!!

The right solution is to add an event to the class containing
RotateDevice and have whatever code that now set reachedPosition
call the delegates assigned to the event.

That obviously requires the ability to change the code. Either
because you have the source and can modify and rebuild or because
you have license and the skills to decompile, modify and recompile.

Arne
 
A

Arne Vajhøj

If this is the only (awful) API, you are stuck with polling. I.e.
waiting in an infinite loop until your variable is true or you don't
want to wait any longer.



while (!Volatile.Read(ref reachedPosition))
Thread.Sleep(1);

This pauses for one time slice after each check - at least with Windows
kernel - and therefore does not hog the CPU.

Smart. Volatile.Read avoid the need for reachedPosition to be
volatile.

Arne
 
A

Anders Eriksson

while (!Volatile.Read(ref reachedPosition))
Thread.Sleep(1);

This pauses for one time slice after each check - at least with Windows
kernel - and therefore does not hog the CPU.
OK, this works!
Thank you very much!

// Anders
 
A

Anders Eriksson

while (!Volatile.Read(ref reachedPosition))
Thread.Sleep(1);

This pauses for one time slice after each check - at least with Windows
kernel - and therefore does not hog the CPU.
I moved the waiting to the dll and now the Form is blocked. Is this the
way it is or ?

// Anders
 
A

Arne Vajhøj

I moved the waiting to the dll and now the Form is blocked. Is this the
way it is or ?

You should not do something like this in the event thread of a GUI.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top