From: caw@miroc.Chi.IL.US (Christopher A. Wichura)
Newsgroups: comp.sys.amiga.programmer
Subject: Re: HELP!!!!
Distribution: global
X-NewsSoftware: GRn-beta 1.16g (10.12.92) by Michael B. Smith & Mike Schwartz
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Date: 27 Dec 92 17:54:32 CST

In article <BzwyMw.2s0@ccu.umanitoba.ca> umbadiu0@ccu.umanitoba.ca (Ted Babiuk) writes:
> *sigh*....its 4:09 and I'm at wits end with this....

I find that it usually helps to get some sleep at this point.  You wake up
and all your problems seem rediculously simply and you can't figure out why
you couldn't come up with the solution before... :-)

> What i'm trying to do is start a new task called 'cycle' which does some
> color cycling while my main program is running.....now.....
>
> Address =LoadSeg("df0:Cycle");  loads the code...and.....
> X = CreateProc("Task_name",Address,priority,stack_size) start it....

Reasonable.  However, having the cycle code in a seperate load module is not
necessary.  Instead, just define the cycle code as a function in your c code
that uses __saveds.  I.e., something like:

void __saveds CycleProc(void);

> but since i need a pointer to the viewport used in the main program in the
> new task 'cycle' to modify colors, how do i pass it???
>
> CreateProc starts a process, but how do you pass data to it....

This is easy to do.  When a process is created it automatically has a message
port called pr_MsgPort.  Normally this is not usable by the application since
dos.library uses it for you behind your back and gets very unhappy when
someone dinks around with it when there are packets pending (for example).
However, when your process is first created it isn't doing any dos calls.  So
what you do is

	stuct MyMessage {
		struct Message mm_Msg;
		struct ViewPort *mm_ViewPort;
	} msg;

	proc = (struct Process *)FindTask(NULL);
	WaitPort(proc->pr_MsgPort);
	msg = (struct MyMessage *)GetMsg(proc->pr_MsgPort);
	viewport = msg->mm_ViewPort;
	ReplyMsg((struct Message *)msg);

What the code that is launching the process does is after CreateProc() it
checks to see if the return it got is zero.  If it is then the process wasn't
started to panic as appropriate.  If it's non-zero then you use this pointer
to PutMsg() your msg to the child.  You then wait for the child to reply
before going on.  After this you should never use pr_MsgPort for your own
purposes again.

Note that this method is essentially what Workbench itself uses to send a
startup message to programs it launches.

You can put anything you want in your startup message.  I often find that is
useful to have a return code field.  The child sets this before replying the
message.  If the child initializes ok then it sets this to zero.  Otherwise
it sets it to some error condition, replys the message and then terminates.
The launching process then checks the return code after the child has replied
the startup message to determine if the child initialized properly and panics
as appropriate when the child fails to start.

Unfortunately, I can't recommend any specific pieces of code that implement
this method for you to look at.  I've used it myself, but that's for a
program I 1) haven't released yet and 2) can't release the source to since it
makes use of things that are currently still covered by my NDA agreement with
C= (which also partly explains why reason 1 is true :->).

Hope the above helps.

-=> CAW

--

Christopher A. Wichura                Multitasking.  Just DO it.
caw@miroc.chi.il.us  (my amiga)                          ...the Amiga way...
u12401@uicvm.uic.edu (school account)
Newsgroups: comp.sys.amiga.programmer,comp.sys.amiga.misc