C#finally块不执行

public void startSendViedo(string IP, int port)
{
try
{
ms = new MemoryStream();

IDataObject data;
Image bmap;

//复制图片到剪贴板
SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0);

//从剪贴板获取图片并将其转化成bitmap
data = Clipboard.GetDataObject();

if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
{
bmap = ((Image)(data.GetData(typeof(System.Drawing.Bitmap))));
bmap.Save(ms, ImageFormat.Bmp);
}

picture_out.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arrImage = ms.GetBuffer();
tc = new TcpClient(IP, port);//连接到对方计算机
myns = tc.GetStream();
bw = new BinaryWriter(myns);
bw.Write(arrImage);//发送图片到对方计算机
ms.Flush();
bw.Flush();
myns.Flush();
ms.Close();
bw.Close();
myns.Close();
tc.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Video Conference Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
timer.Enabled = false;
stopvideo.Enabled = false;
startvideo.Enabled = true;
}
}
断点跟踪,catch与finally块呈现灰白,只执行catch块,finally直接跳过,只好把timer.enable=false放在catch块中处理;
tc = new TcpClient(IP, port);//连接到对方计算机执行到这条语句异味常,跳到catch块中执行,catch运行完没执行finally块,而是运行timer1_Click()这个函数。

第1个回答  2010-03-30
这是不可能地,兄弟,我看你里面有个timer,估计是这个原因,它不是没执行,是有可能没在你当前的线程里。他执行的时候,你没看见。
第2个回答  2010-03-30
关闭部分应该写到finally块中.
在执行关闭前如果发生异常,那么程序将进入catch块而跳过关闭.本回答被提问者采纳
第3个回答  2010-03-30
你断点跟踪一下程序看看

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网