Hey there,
I have a bunch of code that works on the mac but fails in windows and I’m not sure how to resolve it.
I am downloading a file from the web to the temp directory, and in the windows case, trying to run it. I think the call is failing in windows because the OS wont allow the file to be used by my program and run at the same time. The problem is, as neither File or OutputStream have any concept of close, I can’t figure out to modify, or even StartAsProcess() a file I have just written.
Here is the code, it’s broken into two functions – a Thread::Run that does the download, and then another function that grabs the file and attempts to open it once the download has succeeded.
Here is the download…
void WebDownloadThread::run()
{
bool failed = false;
m_downloadProgress = 0.0;
m_stateLock.enter();
m_state = Busy;
m_stateLock.exit();
m_dataLock.enter();
InputStream* input = m_url.createInputStream(m_isPost,
NULL,
NULL);
String filename = m_url.getSubPath();
filename = filename.substring(filename.lastIndexOfChar('/') + 1);
filename = "MyApp" + filename;
m_downloadedFile = File::createTempFile(filename);
if(input){
int total = input->getTotalLength();
FileOutputStream* output = m_downloadedFile.createOutputStream(0x8000);
while(!input->isExhausted()){
output->writeFromInputStream(*input, 0x8000);
int position = input->getPosition();
m_progressLock.enter();
m_downloadProgress = (float) position / (float) total;
m_progressLock.exit();
}
output->flush();
m_downloadProgress = 1.0;
}
else
failed = true;
m_dataLock.exit();
m_stateLock.enter();
if(failed)
m_state = Failed;
else
m_state = Succeeded;
m_stateLock.exit();
}
And Here is the open:
void SoftwareUpdateManager::finishSoftwareUpdate(){
if(m_softwareUpdateDownloadThread->getState() != WebDownloadThread::Succeeded)
return;
File downloadedFile = m_softwareUpdateDownloadThread->getDownloadedFile();
if(downloadedFile.startAsProcess())
{
LOG_DEBUG("Succesffully launched new installer\n");
}
else
{
// land up here every time in windows
LOG_DEBUG("Failed to launch new installer\n");
}
}
The downloaded file will open from the Explorer only after my program quits, with the complaint that another program is using the file. That is why I think it has something to do with me not explicitely closing it. Is there a way can force my variable to close the file?
Thanks Jules,