Re: Starting and Stopping an Automated Process in Win32Lib

new topic     » goto parent     » topic index » view thread      » older message » newer message
euphoric said...

I want to be able to click a button which starts an automated process (within the same program/window), but also have a "Stop" button that will stop it.

Can somebody (like Derek) put together a small sample of how I might accomplish that? Or point to a demo that does this already?

Thanks in advance! smile

I'm not quite sure what you are asking for, sorry.

I'm guessing you want to click a button to start an independent thread within the current process, and press a button to signal that thread to stop. Depending on how independent you wish the thread to be, it could be done quite simply. Here is one suggestion ...

include win32lib.ew 
include std/filesys.e 
include std/text.e 
 
integer hmainwin 
integer hstartpath 
integer hdirresult 
integer hbutton 
integer hwriter 
integer startstop 
 
constant STOPPED = 0, 
         STARTED = 1 
          
function ShowFile(sequence path, sequence dirent) 
	if startstop = STARTED then 
		-- keep showing files. 
		if find('d', dirent[D_ATTRIBUTES]) then 
			addItem(hdirresult, '[' & path & SLASH & dirent[D_NAME] & ']' ) 
		else 
			addItem(hdirresult, path & SLASH & dirent[D_NAME]) 
		end if 
		setIndex(hdirresult, -1) 
		 
		-- allow other events to occur before getting the next file to show. 
		doEvents(0) 
	 
		return 0 
	else 
		-- Something has told me I have to tell walk_dir to stop. 
		return 1 
		 
	end if 
	 
end function 
 
procedure Click_Button(integer id, integer event, sequence parms) 
 
	sequence path 
	if startstop = STOPPED then 
		path = getText(hstartpath) 
		if length(path) = 0 then 
			return 
		end if 
		 
		startstop = STARTED 
		setText(hbutton, "Stop") 
		 
		eraseItems(hdirresult) 
		 
		setFocus(hwriter) -- Just to show user they can type stuff here now. 
		if length(getText(hwriter)) = 0 then 
			setText(hwriter, "Begin typing here now.") 
			setIndex(hwriter, {1,0}) 
		end if 
		 
		-- begin showing files. 
		walk_dir(path, routine_id("ShowFile"), 1) 
 
		startstop = STOPPED 
		setText(hbutton, "Start") 
 
	else 
		startstop = STOPPED 
		setText(hbutton, "Start") 
	end if	 
end procedure 
 
procedure closer(integer id, integer event, sequence parms) 
	if startstop = STARTED then 
		-- Thus walk_dir is currently running so I have to signal it to stop. 
		startstop = STOPPED 
		doEvents(0)	-- give walk_dir a chance. 
	end if 
end procedure 
 
procedure main() 
	startstop = STOPPED 
	 
	hmainwin   = create(Window, "The Window", 0, 0, 0, 800, 600, 0) 
	setHandler(hmainwin, w32HClose, routine_id("closer")) 
	 
	             create(LText, "Path:", hmainwin,  5, 10, 40, 18, 0) 
	hstartpath = create(EditText, "",  hmainwin,  50, 10, 400, 18, 0) 
	 
	             create(LText, "Files", hmainwin,  5, 30, 40, 18, 0) 
    hdirresult = create(ListBox, "",    hmainwin,  5, 50, 500, 500, 0) 
     
    hbutton    = create(Button, "Start", hmainwin, 5, 530, 70, 22, 0) 
    setHandler(hbutton, w32HClick, routine_id("Click_Button")) 
     
    hwriter    = create(MleText, "",  hmainwin, 515, 10, 200, 400, 0) 
     
    WinMain( {hmainwin, hstartpath}, Normal) 
end procedure 
 
main()  

This program gets the file names from the starting path and adds them to the list. While it is doing that, you are free to use the other controls ... eg. write in the textbox and press the stop button.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu