八月 2007 - 博客

assert使用的一个注意点

在C++程序中使用断言是一个良好编程习惯, 但是使用assert的时候有一点需要注意, assert或者ASSERT在VC++中是以宏的方式实现的 注意到它的定义,它只有在DEBUG模式下才起作用, 我们使用断言的时候, 断言中应该只包含判断, 而不应该有其他行为. 这一次我们程序开发中就出现了这样一个问题: 程序在DEBUG下执行没有任何问题, 可是在Release下执行的时候却总是出错, 后来发现原来我们有一个初始化函数放在断言里面执行了, 既有一个函数Init, 返回的是布尔值, 结果我们直接这样使用断言了assert(Init()), 这在Release下面根本就不会出现Init了.自然会出错.

Posted 作者 zhaoyang0618 | with no comments

Applications=Code+Markup 读书笔记3 第一章 The Application And The Window (2)

 前面一节主要是讨论Window, 这一节讨论Application. 一个应用程序只能有一个Application实例 . Application中有几个事件非常有用. OnStartUp(StartUp事件)在调用了Application的Run方法之后马上执行, 而OnExit(Exit事件)方法是在Run方法返回的时候执行, 这两个方法非常适合用于初始化和释放资源.

 OnSessionEnding方法用于处理SessionEnding事件, 在关闭Windows或者关机的时候激发该消息,  该消息中有一个参数SessionEndingCancelEventArgs, 其中有一个属性Cancel, 如果设置该属性为false, 可以阻止WIndows关闭. 这个消息只有在Windows Application模式下编译才能起作用. SessionEndingCancelEventArgs中有一个属性ReasonSessionEnding表明目前是在注销用户还是在关机.


/----------------------------------------------
// InheritTheApp.cs (c) 2006 by Charles Petzold
//----------------------------------------------
using System;
using System.Windows;
using System.Windows.Input;

namespace Petzold.InheritTheApp
{
class InheritTheApp : Application
{
[STAThread]
public static void Main()
{
InheritTheApp app = new InheritTheApp();
app.Run();
}
protected override void OnStartup
(StartupEventArgs args)
{
base.OnStartup(args);

Window win = new Window();
win.Title = "Inherit the App";
win.Show();
}
protected override void OnSessionEnding
(SessionEndingCancelEventArgs args)
{
base.OnSessionEnding(args);

MessageBoxResult result =
MessageBox.Show("Do you want to save your data?",
MainWindow.Title,
MessageBoxButton.YesNoCancel,
MessageBoxImage
.Question, MessageBoxResult.Yes);

args.Cancel = (result == MessageBoxResult.Cancel);
}
}
}

 我们可以从命令行启动程序, 要接受命令行中的参数, 需要修改Main如下:
public static void Main(string[] args)
这些命令行中的参数还可以在OnStartUp的参数StartupEventArgs的Args属性中访问到.
 

 

更多内容