Sheen Space

Posts Tagged ‘C#

Start-up process of WPF application

leave a comment »

This is my WPF study note. I don’t guarantee it is error-free. Please kindly post comment when you think anything is wrong.

There is a top-level user-defined application class derived from System.Windows.Application. Let’s call it MyApp. After compilation, C# compiler generates two functions for MyApp class:

  • static void Main()
  • void  InitializeComponent()

Compiler prohibits you from defining the same functions.

MyApp.Main() calls MyApp.InitializeComponent(), and then calls System.Windows.Application.Run().

MyApp.InitializeComponent() has information of XAML file of main window of the application, and calls System.Windows.Application::set_StartupUri() with the XAML file path. It knows the XAML file path of main window because the file path is specified in <Application/StartupUri> in MyApp.xaml.

System.Windows.Application.Run() executes like normal WinForm code, initialising the main window and entering window message loop.

Written by Ying

23/05/2012 at 00:09

Posted in DotNet, Technology, Windows

Tagged with ,

Unnecessary relink caused by “/DYNAMICBASE” in Visual C++ 2008

leave a comment »

I am working on a big C++ solution which includes many DLL and EXE projects. It is created before Visual C++ 2008. Now we migrate to VC 2008, and I create new DLL projects in the solution.

I find that everytime there is change in my new DLL code, no matter how trivial it is, for example, only comment change in .cpp file, all dependent projects relink. It happens on my new projects, not those existing projects. So I spent some time comparing project settings, and found the cause:

/DYNAMICBASE (Use address space layout randomization)

By default, /DYNAMICBASE is on.

This option modifies the header of an executable to indicate whether the application should be randomly rebased at load time.

Address space layout randomization is supported on Windows Vista.

There is also another technical article from MSDN blog:

Address Space Layout Randomization in Windows Vista

This is a new program security enhancement setting introduced in VC 2008. It causes compiled binary to change everytime. Switching this setting off makes problem gone.

If your solution is big and you don’t need this level of security, switch it off for all your new projects.

Written by Ying

14/10/2011 at 10:33

Posted in C++, Programming, Technology

Tagged with , , ,

Sutter’s another great C++ talk

leave a comment »

“Writing modern C++ code: how C++ has evolved over the years”
http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-835T/player?w=960&h=544

Written by Ying

07/10/2011 at 22:08

Posted in C++, Programming, Technology

Tagged with

Web Development – Study Notes – Set it up

leave a comment »

Set up a fairly helpful environment

I want to start learning web programming. As a system programmer, I once pretended web development is another world I don’t need to know about. This becomes more and more difficult. I finally make my decision to move into this strange territory.

I am a bit thorough (you can say perfectionist), so I would like to make a friendly and clean environment for my learning and development. I choose to:

  • Create virtual development machine using VirtualBox (currently version 3.2.8).
  • Install Visual Studio 2010 on it.

I don’t want to buy web hosting service because it is not cheap, and it doesn’t make sense to pay for a up-and-running environment when you want to learn from scratch. And I don’t like the feeling that some details are hidden from me (perfectionist you remember?). I choose to:

  • Set up another dedicated virtual web server using Windows Server 2008 R2.
  • Use IIS7.5 to host website.
  • Use ASP.NET 4

After I created a small web project in VS2010, I realized I need some way to deploy it to the web server for testing. It is sweet that VS2010 has this feature. But before deployment, I need to enable FTP Publishing for my website in IIS Configuration.

Because of my choice of hosting website at home, which means dynamic IP, I have to resolve imminent problem – Dynamic DNS Mapping, that is to have a fixed domain name mapping to my consistently changing IP. My vote goes to www.dyndns.org this time, who provides free dynamic DNS service. Later if I own my private domain name I can even let it use that one.

Written by Ying

27/09/2010 at 16:50

Visual Studio 2010 Web自动发布

with one comment

ScottGu的一篇教我们怎么使用VS2010自动发布更新的网站内容的Blog:
http://weblogs.asp.net/scottgu/archive/2010/07/29/vs-2010-web-deployment.aspx

他的VS2010和.NET4文章集合在这里,非常有用:
http://weblogs.asp.net/scottgu/archive/2009/08/25/vs-2010-and-net-4-series.aspx

Written by Ying

25/09/2010 at 21:52

Posted in Programming, Technology

Tagged with ,

回调(Callback), 委托(Delegate), 事件(Event)

leave a comment »

本文通过认识Observer Pattern,以及对比C++,C#对这个模式的实现来理解C#中的委托(delegate)和事件(event)。

Observer Pattern设计模式

http://en.wikipedia.org/wiki/Observer_pattern Observer Pattern绝不是什么新东西。大家能想到的最早最低级别的实际例子是什么?Intel x86系统中的中断向量表是我能想到的最低级别的实现。整个计算机就是subject,可以随时产生各种事件,INT指令就是事件触发,中断向量表中对应的中断处理函数就是observer对特定事件的反应。 下面是我写的两个小例子程序,用来演示Observer Pattern在C++和C#中是如何实现的。只是一个概念展示,程序不见得有实际用途。两个程序类似,都定义了一个class Subject,和对应的两个Observer。很容易看懂,就不做解释了。

回调函数(C++)

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

// Subject class that accepts callback function pointer
class Subject
{
	// Type declaration
	// Define Callback function type, which is essentially function pointer
	typedef void (*Callback)(void* subject, void* arg);
private:
	// List of Callback function pointers 
	list<Callback> _callbacks;
public:
	void RegisterNotification(Callback callback)
	{
		list<Callback>::iterator it = find(_callbacks.begin(), _callbacks.end(), callback);
		if (it == _callbacks.end())
			_callbacks.push_back(callback);
	}
	
	void UnregisterNotification(Callback callback)
	{
		list<Callback>::iterator it = find(_callbacks.begin(), _callbacks.end(), callback);
		if (it != _callbacks.end())
			_callbacks.erase(it);
	}
	
	// Invoke all callback functions
	void Notify()
	{
		if (_callbacks.empty())
			cout << "Nobody to notify" << endl;
		
		for (list<Callback>::iterator it = _callbacks.begin(); it != _callbacks.end(); ++it)
			(*it)(this, 0);
	}
	
	void Run()
	{
		// Do some work
		Notify();
	}
};

// Concrete observer
class Observer1
{
public:
	static void Notify(void* subject, void* arg)
	{
		cout << "Observer1 notified" << endl;
	}
};

// Concrete observer
class Observer2
{
public:
	static void Notify(void* subject, void* arg)
	{
		cout << "Observer2 notified" << endl;
	}
};

int main(int argc, char* argv[])
{
	Subject subject;
	
	subject.RegisterNotification(Observer1::Notify);
	subject.RegisterNotification(Observer2::Notify);
	
	subject.Run();
	
	subject.UnregisterNotification(Observer1::Notify);
	subject.UnregisterNotification(Observer2::Notify); subject.Run();
	
	return 0;
}

委托和事件(C#)

using System;
using System.Collections.Generic;
using System.Text;

namespace ObserverCs
{
	// Subject class that accepts callback function pointer
	class Subject
	{
		// Type declaration
		// Define Callback function type, which is essentially function pointer
		public delegate void Callback(object subject, object arg);
		
		// Event object public event Callback C;
		public void Run()
		{
			// Do some work
			if (C != null)
				C.Invoke(this, null);
			else Console.WriteLine("Nobody to notify");
		}
	}
	
	// Concrete observer
	class Observer1
	{
		public static void Notify(object subject, object arg)
		{
			Console.WriteLine("Observer1 notified");
		}
	}
	
	// Concrete observer
	class Observer2
	{
		public static void Notify(object subject, object arg)
		{
			Console.WriteLine("Observer2 notified");
		}
	}
	
	class Program
	{
		static void Main(string[] args)
		{
			Subject subject = new Subject();
			subject.C += Observer1.Notify;
			subject.C += Observer2.Notify;
			
			subject.Run();
			
			subject.C -= Observer1.Notify;
			subject.C -= Observer2.Notify;
			
			subject.Run();
		}
	}
}

C#委托和事件辨析

有些C#初学者对于委托和事件的理解不够明晰深刻。通过认识Observer Pattern和上面两个例子代码的对比,应该认识就会很深刻了。下面我们把两个例子中的关键代码行放在一起仔细对比一下:

// C++
typedef void (*Callback)(void* subject, void* arg);
// C#
delegate void Callback(object subject, object arg);

// C++
list&lt;Callback&gt; _callbacks;
// C#
event Callback C;

// C++
subject.RegisterNotification(Observer1::Notify);
// C#
subject.C += Observer1.Notify;

委托就是对拥有相同签名的函数/方法的类型定义;事件就是委托实例的集合,包含0个或多个委托函数实例。事件这个名字稍微有点迷惑性。他更多的表达的是这个委托集合在应用上的一般目的(处理事件)。

Written by Ying

21/09/2010 at 14:29

Question about .NET GC

leave a comment »

A question at StackOverflow site: http://stackoverflow.com/questions/1716353/object-finalize-override-and-gc-collect

I have had similar question about GC and raised it in this thread:

http://stackoverflow.com/q/3745507/451875

quoted below:

Here is another great article about GC may occur at unexpected point of code execution:

Lifetime, GC.KeepAlive, handle recycling – by cbrumme http://blogs.msdn.com/b/cbrumme/archive/2003/04/19/51365.aspx?wa=wsignin1.0

My question is how can I reproduce forced GC at the point mentioned in the article? I tried to put GC.Collect() at beginning of OperateOnHandle(), and defined destructor for class C but seems not working. Destructor is invoked always at end of program.

After others’ help now I can reproduce this behavior. Refer to the question I raised above for update and code I put there. The article written by cbrumme is based on .NET 1.0 and Microsoft has provided elegant solution from .NET 2.0 onwards, that is SafeHandle. Excuse my ignorance.

Written by Ying

19/09/2010 at 11:31