[英]Unpacking a .cab file and then closing the running process on windows CE 6.0 with .net mobile 3.5
我有一个Windows CE 6.0程序,该程序使用.cab文件进行安装。 它并入了http://msdn.microsoft.com/zh-cn/library/aa446487.aspx中的指南,以支持自动自我更新。
该程序可以很好地检查更新,它会按应有的方式下载更新,但是当我尝试使其解压缩刚刚下载的.cab文件时,它将失败。
这是我的代码:
void ResponseReceived(IAsyncResult res)
{
try
{
_mResp = (HttpWebResponse)_mReq.EndGetResponse(res);
}
catch (WebException ex)
{
MessageBox.Show(ex.ToString(), "Error");
return;
}
// Allocate data buffer
_dataBuffer = new byte[DataBlockSize];
// Set up progrees bar
_maxVal = (int)_mResp.ContentLength;
pgbDownloadBar.Invoke(new EventHandler(SetProgressMax));
// Open file stream to save received data
_mFs = new FileStream(@"\Application\CCOptimizerSetup.cab",
FileMode.Create);
// Request the first chunk
_mResp.GetResponseStream().BeginRead(_dataBuffer, 0, DataBlockSize,
OnDataRead, this);
}
void OnDataRead(IAsyncResult res)
{
// How many bytes did we get this time
int nBytes = _mResp.GetResponseStream().EndRead(res);
// Write buffer
_mFs.Write(_dataBuffer, 0, nBytes);
// Update progress bar using Invoke()
_pbVal += nBytes;
pgbDownloadBar.Invoke(new EventHandler(UpdateProgressValue));
// Are we done yet?
if (nBytes > 0)
{
// No, keep reading
_mResp.GetResponseStream().BeginRead(_dataBuffer, 0,
DataBlockSize, OnDataRead, this);
}
else
{
// Yes, perform cleanup and update UI.
_mFs.Close();
_mFs = null;
Invoke(new EventHandler(AllDone));
}
}
private void AllDone(object sender, EventArgs e)
{
Cursor.Current = Cursors.Default;
DialogResult dialogresult = MessageBox.Show(@"CCOptimizer has finished downloading. Open now?", "Download complete", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
switch (dialogresult)
{
case DialogResult.OK:
Application.Exit();
Dispose();
Close();
Invoke(new EventHandler(StartProcess));
break;
case DialogResult.Cancel:
Form1 oForm = new Form1();
oForm.Show();
Hide();
break;
}
}
private void StartProcess(object sender, EventArgs e)
{
Process.Start(@"\Application\CCOptimizerSetup.cab", null);
}
虽然不行。 无论我在哪里调用StartProcess()
,它要么只是关闭而没有任何消息,要么在解压缩过程中出现错误,表明我要更新的文件之一仍在使用中。 在调试过程中,它只是停止程序。 如果我尝试安装的版本,它会短暂地闪烁一个窗口,然后旋转WaitCursor锁定机器,并需要重新启动,尽管之后可以很好地安装它。
如何关闭当前程序并打开.cab文件,而不必重新启动计算机?
您的更新逻辑存在缺陷,可能是您要更新的应用是检查更新的事物(问题尚不完全清楚,但行为表明确实如此)。
应用程序无法直接更新自身,因为出于显而易见的原因,您不能替换已经加载并正在运行的程序集。
一般来说,有两种方法可以解决此问题。 两者都需要第二个可执行文件(在许多情况下,无论如何我都已经拥有它作为看门狗应用程序)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.