
-----------------------------------
Kelsey
Thu Jan 06, 2005 6:05 pm

Ending a Process?
-----------------------------------
After I use fork to begin running a process, how do I end it?

-----------------------------------
MihaiG
Thu Jan 06, 2005 6:14 pm


-----------------------------------
possibly 

end fork
???

-----------------------------------
Cervantes
Thu Jan 06, 2005 6:50 pm


-----------------------------------
possibly 

end fork
???
Nope.  Use return and a flag variable.


Yeah, I should have said it earlier, guess I forgot, sorry.

but things like this:

var flag : boolean := false

process doSomething
    if flag = true then
        return
    end if
    put "Hello"
end doSomething

for rep : 1 .. 100
    put "Hi"
    fork doSomething
    if rep > 20 then
        flag := true
    end if
end for


return will terminates the process.


-----------------------------------
Martin
Thu Jan 06, 2005 6:54 pm


-----------------------------------
Don't use processes.

-----------------------------------
Kelsey
Thu Jan 06, 2005 7:03 pm


-----------------------------------
Don't use processes.

Then how would you suggest I run a timer in my program?


And to the other two of you, thnaks!

-----------------------------------
Cervantes
Thu Jan 06, 2005 7:10 pm


-----------------------------------
Don't use processes.
Then how would you suggest I run a timer in my program?


You could put it in your main loop.  What do you mean when you say a "timer", however?  A timer since the program has started (or since some event occured)?  Time.Elapsed, inside your main loop, will work fine.

-----------------------------------
Kelsey
Thu Jan 06, 2005 7:17 pm


-----------------------------------


You could put it in your main loop.  What do you mean when you say a "timer", however?  A timer since the program has started (or since some event occured)?  Time.Elapsed, inside your main loop, will work fine.

It starts when an even occurs. How do I encorperate Time.Elapsed so it can be outputed in the run window?

-----------------------------------
Cervantes
Thu Jan 06, 2005 7:24 pm


-----------------------------------
huh?
put Time.Elapsed

Or, a little more complex:


for i : 1 .. 100
    delay (10)
    locate (1, 1)
    put "Current Time: ", Time.Elapsed
end for
var event_occured_at := Time.Elapsed
locate (2, 1)
put "Event \"for loop complete\" occured at ", event_occured_at

loop
    locate (3, 1)
    put "Time Since event occured : ", Time.Elapsed - event_occured_at
end loop


-----------------------------------
Martin
Thu Jan 06, 2005 7:26 pm


-----------------------------------
Well, you have your main loop keep track of the time that has elapsed since the beginning of the program.

Each event is then tied to a start time, and has a life, and this is all checked in the main loop. It's much cleaner.

In pseudocode:

while (not gameOver())
    totalTime = Time.Elapsed - startTime
    checkEventsForTimeBasedConditions()
    ....
end while

-----------------------------------
Kelsey
Thu Jan 06, 2005 7:28 pm


-----------------------------------
huh?
put Time.Elapsed

Or, a little more complex:


for i : 1 .. 100
    delay (10)
    locate (1, 1)
    put "Current Time: ", Time.Elapsed
end for
var event_occured_at := Time.Elapsed
locate (2, 1)
put "Event \"for loop complete\" occured at ", event_occured_at

loop
    locate (3, 1)
    put "Time Since event occured : ", Time.Elapsed - event_occured_at
end loop


This doesn't really seem like what I'm trying to do, but thanks for explaining that. Why is it so bad to use processes anyways?

-----------------------------------
Martin
Thu Jan 06, 2005 7:34 pm


-----------------------------------
Processes behave somewhat inconsistantly and unpredictably, and are often used inefficiently.

The basic idea of writing a program is to perform as few calculations as necissary, and when people use processes, 99% of the time, they are calculating something over and over, and only using that calculation a couple of times.

-----------------------------------
Kelsey
Thu Jan 06, 2005 7:43 pm


-----------------------------------
Processes behave somewhat inconsistantly and unpredictably, and are often used inefficiently.

The basic idea of writing a program is to perform as few calculations as necissary, and when people use processes, 99% of the time, they are calculating something over and over, and only using that calculation a couple of times.


 process timer %Timer for the game, runs parallel later when game is begun
    loop
        time_count := time_count + 1
        time_count2 := intstr (time_count) 
        delay (1000)
        drawfillbox (140, 240, 180, 260, 7)
        Font.Draw (time_count2, 145, 245, font, brightred)
        exit when time_count = 999
    end loop
end timer

. . .
%Bunch of other stuff here
. . .

loop
        Mouse.Where (x, y, b)
        if b = 1 and x < 120 and x > 80 and y < 275 and y > 235 then
            fork timer
            drawfillbox (80, 235, 120, 275, 25)
            Font.Draw ("STOP", 83, 250, font, black)
. . . 
Bunch more stuff
. . . 
end loop


Basically, when you click the button to start the game (Minesweeper), the timer needs to begin. What I wanted to do was end the process when the button is clicked again (it changes to a stop button once the game begins).

I have a lot of conditions that need to be checked continuosly throughout the loop, so adding a delay in there for my time to show up every second wouldn't be so great. I think using a process here works :? .

-----------------------------------
Cervantes
Thu Jan 06, 2005 8:00 pm


-----------------------------------

I have a lot of conditions that need to be checked continuosly throughout the loop, so adding a delay in there for my time to show up every second wouldn't be so great. I think using a process here works.

Nah:

var elapsedSeconds := 0
loop
    %do stuff
    %do drawing stuff
    if Time.Elapsed >= elapsedSeconds * 1000 then
        elapsedSeconds += 1
        locate (1, 1)
        put elapsedSeconds
    end if
    %have your View.Update and delay here
end loop


:)

-----------------------------------
Kelsey
Thu Jan 06, 2005 8:05 pm


-----------------------------------
Thank-you!
