CopyFile and Progressbar

Discuss GUI programming with the RapaGUI plugin here
Post Reply
walkero
Posts: 17
Joined: Fri Jul 29, 2022 11:59 pm

CopyFile and Progressbar

Post by walkero »

Hello guys.
I am having a RapaGUI window with a progressbar, which I am trying to fill up while CopyFile is copying files. I do this on Linux with Gnome 3.
I use the Callback for CopyFile, but it seems that while the CopyFile is working, although I get the right information in DebugMessages (copied and filesize), it seems that RapaGUI is freezed. I even get a Linux window that asks me to terminate the application, as it seems frozen.

Here is the callback I am using

Code: Select all

Function p_CopyCallback(msg)
	Switch msg.action
	Case #COPYFILE_STATUS:
		Local percent = Ceil(msg.copied / msg.filesize * 100)
		moai.Set("prgbarCopy", "Level", percent)
		DebugPrint("Now copying", FilePart(msg.source), "to", PathPart(msg.destination), ": ", msg.Copied, " of ", msg.Filesize)
	EndSwitch
	Return(False)
EndFunction
which I call like:

Code: Select all

CopyFile(FullPath(srcPath$, fname$), destPath$, {Callback = p_CopyCallback})
Currently, the progressbar get to 100 when the copy of the file is finished. It freezes there and then updates when the next copy is complete.
Does anyone know if I need to do something else to have it working properly, showing the progressbar moving while the file is copied?
plouf
Posts: 712
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: CopyFile and Progressbar

Post by plouf »

Yes
You should update rapagui view e.g. progressbar
with RunCallback()
Christos
plouf
Posts: 712
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: CopyFile and Progressbar

Post by plouf »

hm you are right, CopyFile() it self is an exception and updates itself
i try following example and progressbar works (windows 11)

Code: Select all

@REQUIRE "RapaGUI"
filesource$="H:\\Users\\plouf\\Downloads\\a.exe"
filedest$ = "h:\\"

moai.CreateApp([[
<?xml version="1.0" encoding="iso-8859-1"?>
<application>
	 <window title="Test program">
			<vgroup>
		    <button id="copyfile">Copy File</button>
				<progressbar id="busybar1" max="100"/>
			</vgroup>
	 </window>
</application>
]])

Function p_EventFunc(msg)
	If msg.id="copyfile"
		CopyFile(filesource$, filedest$, {CallBack = p_CopyCallback})
	EndIf
EndFunction


Function p_CopyCallback(msg)
	If msg.action = #COPYFILE_STATUS
		percent = Ceil(msg.copied / msg.filesize * 100)
		moai.Set("busybar1","level",percent)
	EndIf
EndFunction	

InstallEventHandler({RapaGUI = p_EventFunc})

Repeat
	WaitEvent
Forever 
Christos
walkero
Posts: 17
Joined: Fri Jul 29, 2022 11:59 pm

Re: CopyFile and Progressbar

Post by walkero »

Thank you plouf for your reply.
On Manjaro with Gnome 3 your example doesn't work. It starts the copy of the file and the progress bar remains empty It fills to the 100% when the copy is finished. Which is exactly what my code does as well.
User avatar
airsoftsoftwair
Posts: 5919
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: CopyFile and Progressbar

Post by airsoftsoftwair »

walkero wrote: Sat Mar 07, 2026 11:25 am On Manjaro with Gnome 3 your example doesn't work. It starts the copy of the file and the progress bar remains empty It fills to the 100% when the copy is finished. Which is exactly what my code does as well.
You must use RunCallback() to make this work everywhere. Even if plouf's version works correctly on Windows, it's not a safe approach for all platforms. Set the "Async" tag to TRUE in CopyFile() and then use RunCallback() to repeatedly run a callback that calls ContinueAsyncOperation() and updates the progress bar until the copy operation is finished. This is the only safe way to do it because it's the only method that returns control to the main loop so that the GUI can update itself. If you just update the progress bar from a CopyFile() status callback, the main loop won't get any chance to update the GUI.
Post Reply