归档 十二月 2010

c#进程间通讯方案之IPC通道

最近一直纠结与使用多进程还是多线程来构建程序。多线程的方法似乎不错,但是一个进程可承受的线程数有有限的,并且由于每个线程都与UI有着些许关系,线程的工作大多数时间浪费在阻塞上了,效率实在不是很高。 笔者遂在google上搜索进程间通讯的方案。发现有很多种,其中IPC通道似乎是个不错的选择,支持本机的进程间通讯,可以作为备选方案之一,下面介绍以下基本的编程方法,以作备忘。 首先建立一个IPC通讯中使用的对象,其中MarshalByRefObject 是必须的 using System; namespace Ipctest { public class test:MarshalByRefObject { private int iCount = 0; public int count() { iCount++; return iCount; } public int Add(int x) { iCount += x; return iCount; } } } 接着建一个服务端控制台程序 using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Ipc; namespace Ipctest { class Program { static [...]

php 文件夹大小统计函数

计算文件夹的大小,包括子文件夹,格式化输出文件夹大小、文件数、子文件夹数信息。 <? //代码也可以用于统计目录数 //格式化输出目录大小 单位:Bytes,KB,MB,GB function getDirectorySize($path) { $totalsize = 0; $totalcount = 0; $dircount = 0; if ($handle = opendir ($path)) { while (false !== ($file = readdir($handle))) { $nextpath = $path . ‘/’ . $file; if ($file != ‘.’ && $file != ‘..’ && !is_link ($nextpath)) { if (is_dir ($nextpath)) { $dircount++; $result [...]

php 随机密码生成函数

随机密码生成也是常用的方法,使用mt_srand生成随机种子,密码的长度可以随意定义,最长32位。 <?php mt_srand((double) microtime() * 1000000); function gen_random_password($password_length = 32, $generated_password = “”){ $valid_characters = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789″; $chars_length = strlen($valid_characters) – 1; for($i = $password_length; $i–; ) { //$generated_password .= $valid_characters[mt_rand(0, $chars_length)]; $generated_password .= substr($valid_characters, (mt_rand()%(strlen($valid_characters))), 1); } return $generated_password; } ?><!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”> <html> <head> <title>php 密码生成器 v 4.0</title> <style [...]

php 二分法查找函数

二分法查找函数的php实现 /* * * binarysearch 在数组中查找制定值 * returns false 没找到( 使用 ‘===’ 作比较) * return position 找到 */ function binarysearch( $haystack, $needle ) { if ( !is_array($haystack) ) { return false; } $btm = 0; $top = count($haystack)-1; // just in case not a normal array, but is sorted properly $keys = array_keys($haystack); while ( [...]

    第 2 页,共 2 页12