General Info

Create a Custom Callback Function

If you have an action requiring a status dialog, but the standard status dialog (StatusDlg) is not sufficient, it is possible to create a custom callback function for those actions requiring one.

As an example, we will use a custom callback function to show the progress of a file download:

 

--ShowStatusWindow shows the status window, and sets the dialog's icon
function ShowStatusWindow()
    -- Show the Status Dialog
    StatusDlg.Show(MB_ICONINFORMATION, false);
    -- Show the cancel button
    StatusDlg.ShowCancelButton(true, "Stop Download");
end



--DownloadStatus is the callback function to be used.
function DownloadStatus (BytesRead, FileSize, TransferRate, SecondsLeft, SecondsLeftFormat, Message)
    -- Set the status meter's range
    StatusDlg.SetMeterRange(0, Math.Floor(FileSize/1024));
    -- Set the meter position
    StatusDlg.SetMeterPos(Math.Floor(BytesRead/1024));

    -- Check if the cancel button was pressed
    if StatusDlg.IsCancelled() then
        -- Cancel button was pressed.  Hide status dialog and terminate download
        StatusDlg.Hide();
        return false;
    else
        -- Cancel button was not pressed, determine message to be displayed
        if FileSize == 0 then
            -- Display 'size unknown' message
            StatusDlg.SetStatusText("Size Unknown (" .. Math.Floor(BytesRead/1024) .. "kb downloaded so far)");
        elseif FileSize > 0 then
            -- Display 'downloaded of total' message
            StatusDlg.SetStatusText("Downloaded " .. Math.Floor(BytesRead/1024) .. " kb of " .. Math.Floor(FileSize/1024) .. " kb");
        end
    
        -- Determine if end of download was reached:
        --   Message will = "", the end of the download is a 'busy' state
        --   BytesRead will be > 0, because more than 1 byte will have been transferred
        --   BytesRead will = FileSize, unless FileSize is unknown (0)
        if (Message == "") and (BytesRead > 0) and (BytesRead == FileSize or FileSize == 0) then
           -- The download has completed, hide the status dialog and display complete message
           StatusDlg.Hide();
           Dialog.Message("Download Complete", Math.Floor(BytesRead/1024) .. " kb have been downloaded");
           return false;
        else
            -- The download has not ended, continue to download
            return true;
        end
    end
end




-- Call the ShowStatusWindow() function
ShowStatusWindow();

-- Download the file using a callback function
HTTP.Download("http://www.yourdomain.com/download_folder/file.ext", "c:\\file.ext", MODE_BINARY, 20, 80, nil, nil, DownloadStatus);
 

Note:  ShowStatusWindow() shows the status window and sets the title.  DownloadStatus() is the custom callback function, and is called by HTTP.Download.

Сайт управляется системой uCoz