公告板
预览模式: 普通 | 列表

配了个新电脑 22'宽屏

公司准备再招人,电脑不够了

想用台式机了。刚好把俺的Vostro1500换给小文用
很冲动,昨天想的今天马上就去百脑汇抱回来了,一共5500,可以刷信用卡,还是不错滴

具体配置:

酷睿2 E7200+超频三
威刚 2G/800 *2
技嘉 EP45-DS3L
希捷 500G SATA
七彩虹9600GT 256M
三星T220
SG9500+航嘉冷钻
漫步者音箱
明基DVD等~~

使用Process类重定向输出与错误时遇到的问题

在使用PHP CLI开发交互式程序事遇到的问题,在网上找到的解决办法 http://www.x2blog.cn/BoyTNT/6311.html

【摘要】
    使用Process类重定向时遇到死锁问题,对Process的实现机制进行了一番思考,想看全文就点进去吧。

【全文】

[系统环境]
    .Net Framework 1.1,使用C#开发WinForm程序

[问题描述]
    程序中要调用外部程序cmd.exe执行一些命令行,并取得屏幕输出,使用了Process类,基本代码如下:


    Process process = new Process();
    process.StartInfo.FileName = "cmd.exe";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.CreateNoWindow = true;
    process.Start();

    …………  //一些处理

    process.StandardInput.WriteLine("exit");
    //取输出内容
    String outputString = process.StandardOutput.ReadToEnd();
    process.WaitForExit();
    process.Close();

    实际使用中发现有时程序会无响应,并且是在执行某些特定的命令时才会无响应。跟踪调试发现,问题出在process.StandardOutput.ReadToEnd()上,在这一行按F10,程序不向下执行,也不产生Exception。

[解决过程]
    由于调试时程序不向下执行,猜测ReadToEnd()没有返回,被阻塞住了,查了查MSDN,没有看到有相关内容。又反编了一下mscorlib.dll,看了看StreamReader.ReadToEnd()的实现方法,是这么一段代码:


    public override string ReadToEnd()
    {
        int num1;
        if (this.stream == null)
        {
            __Error.ReaderClosed();
        }
        char[] chArray1 = new char[this.charBuffer.Length];
        StringBuilder builder1 = new StringBuilder(this.charBuffer.Length);
        while ((num1 = this.Read(chArray1, 0, chArray1.Length)) != 0)
        {
            builder1.Append(chArray1, 0, num1);
        }
        return builder1.ToString();
    }

    从中看出ReadToEnd()是利用Read()方法实现的,又去MSDN看了,仍然没有提到Read()是阻塞函数,于是只能上网去查一查相关资料,有人提到了类似的问题,并且给出了解决方法:使用两个线程分别做StandardOutput.ReadToEnd()和StandardError.ReadToEnd()。试了一下,果然可以解决。

[后续思考]
    问题虽然解决,不过原理没有弄明白,还要继续深入。查阅相关资料,大致了解了Process的运作模式,它以子进程的形式启动cmd.exe,如果指定了重定向Input、Output或Error,就建立相应的管道在父子进程之间进行通讯。
    基于这些内容以及关于管道的理解,得到以下几条猜测:
    1、input、output、error的三条管道是相互独立的。
    2、管道有大小,空时不可读,满时不可写。
    3、遇到管道不可读写时,相应的进程会阻塞等待可读写为止。
    4、子进程结束前,output流与error流不会结束。

    这样再翻回来考虑之前遇到的问题,可以猜测无响应时是这样一种情况:
    建立Process时指定了Output和Error都重定向,父子进程间就建立了2条管道,cmd.exe不停输出output与error,分别进入两条管道,对output,管道满后,子进程会停下来等待父进程取走数据,父进程的StandardOutput.ReadToEnd()方法正是取数据的,它在管道空时会处于阻塞状态,有数据时就取走,这样子进程会继续写output。
    但是对于error,父进程没有相应的ReadToEnd()方法,很快error的管道就满了,由于无法写error,子进程会停下来等待,但是父进程永远不读,子进程也就永远不再继续,形成死锁。
    对于不出问题的情况,一定是error管道根本就没有写满,这也就解释了为什么命令的内容决定了是否会出现无响应的情况。

    那么直接在StandardOutput.ReadToEnd()后面加一条StandardError.ReadToEnd()行不行呢?答案是不行的,基于上面的猜测与分析,ReadToEnd()方法是阻塞的,在子进程结束前,output流不会关闭,所以StandardOutput.ReadToEnd()会一直等待,造成后面的StandardError.ReadToEnd()方法根本不被执行,还是会产生死锁。所以解决方案里才提到了要建立2个线程,让两个管道的ReadToEnd()方法独立执行。

    再回来考虑我的程序中的情况,之所以没有去读Error,是因为我根本不关心子进程产生的error输出,所以应该有更简单的方法,就是把process.StartInfo.RedirectStandardError置为false,经测试验证,想法是对的, 这样还避免了使用线程。
    在分析的过程中还对process.WaitForExit()方法产生了兴趣,从msdn的描述来看,它的作用是等待子进程结束,应该也是阻塞的,但在我的程序中它在ReadToEnd()方法后面,是否还有实际作用呢?猜测子进程结束后,ReadToEnd()方法才返回,WaitForExit()方法才被调用,但此时它已没有什么用了,肯定立刻返回,向下执行。跟踪调试了一下,验证了自己的想法,于是干脆去掉了这一句,经验证并没有对运行产生影响。

    以上很大部分都是自己的猜测,并不敢保证准确,也许很多地方理解有误,欢迎各位大牛指正。

【全文结束】
【评论1】时间:2006-6-22 10:52:40作者:bood
嗯,分析的很有道理
【评论2】时间:2007-11-12 18:03:11作者:九门提督
存在另外一种情况,即Process.StandardOutput.ReadLine(),正常的屏显输出在Process.StandardError.ReadLine(),对于这种情况,只要改从error管道获取就可以解决。
如Oracle的exp.exe
是否具有普遍性尚未考证。
【评论3】时间:2008-6-6 10:50:44作者:Mac
Very simple method can resolve this problem well:
Use asynchronous read operations. See MSDN.

Follow these steps to perform asynchronous read operations on StandardOutput for a Process :
1. Set UseShellExecute to false.
2. Set RedirectStandardOutput to true.
3. Add your event handler to the OutputDataReceived event. The event handler must match the System.Diagnostics..::.DataReceivedEventHandler delegate signature.
4. Start the Process.
Call BeginOutputReadLine for the Process. This call starts asynchronous read operations on StandardOutput.

When asynchronous read operations start, the event handler is called each time the associated Process writes a line of text to its StandardOutput stream.

php汉字的截取完美解决方案

php汉字的截取完美解决方案 函数巧妙地运用了正则表达式, 用起来很方便, 就像 substr 的用法一样, 可以正向截取也可反相截取, 思路值得学习.
函数巧妙地运用了正则表达式, 用起来很方便, 就像 substr 的用法一样, 可以正向截取也可反相截取, 思路值得学习.

PHP代码:
  1. <?php
  2. function c_substr($string, $from, $length = null){
  3.     preg_match_all('/[x80-xff]?./', $string, $match);
  4.     if(is_null($length)){
  5.         $result = implode('', array_slice($match[0], $from));
  6.     }else{
  7.         $result = implode('', array_slice($match[0], $from, $length));
  8.     }
  9.     return $result;
  10. }
  11. $str = "zhon华人min共和guo";
  12. $from = 3;
  13. $length = 7;
  14. echo(c_substr($str, $from, $length));
  15. // 输出: n华人min共
  16. //还有utf-8的
  17. /*
  18. Regarding windix's function to handle UTF-8 strings:
  19. one can use the "u" modifier on the regular expression so that the pattern string is treated as UTF-8
  20. (available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32).
  21. This way the function works for other encodings too (like Greek for example).
  22. The modified function would read like this:
  23. */
  24. function utf8_substr($str,$start) {
  25. $null = "";
  26.    preg_match_all("/./u", $str, $ar);
  27.    if(func_num_args() >= 3) {
  28.        $end = func_get_arg(2);
  29.        return join($null, array_slice($ar[0],$start,$end));
  30.    } else {
  31.        return join($null, array_slice($ar[0],$start));
  32.    }
  33. }
  34. ?>
  之二:
  1. <?php
  2. /***************************************************/
  3. /*                                                                     */
  4. /*  Author:                                                     */
  5. /*  HomePage: www..com                                          */
  6. /*  Email: phforum@163.com                                             */
  7. /*  QQ:1984412                                                         */
  8. /*                                                                     */
  9. /***************************************************/
  10. function get_substr($string,$start='0',$length='')
  11. {
  12. $start = (int)$start;
  13. $length = (int)$length;
  14. $i = 0;
  15. if(!$string)
  16. {
  17.   return;
  18. }
  19. if($start>=0)
  20. {
  21.   while($i<$start)
  22.   {
  23.    if(ord($string[$i])>127)
  24.    {
  25.     $i = $i+2;
  26.    }
  27.    else
  28.    {
  29.     $i++;
  30.    }
  31.   }
  32.   $start = $i;
  33.   if($length=='')
  34.   {
  35.    return substr($string,$start);
  36.   }
  37.   elseif($length>0)
  38.   {
  39.    $end = $start+$length;
  40.    while($i<$end)
  41.    {
  42.     if(ord($string[$i])>127)
  43.     {
  44.      $i = $i+2;
  45.     }
  46.     else
  47.     {
  48.      $i++;
  49.     }
  50.    }
  51.    if($end != $i-1)
  52.    {
  53.     $end = $i;
  54.    }
  55.    else
  56.    {
  57.     $end--;
  58.    }
  59.    $length = $end-$start;
  60.    return substr($string,$start,$length);
  61.   }
  62.   elseif($length==0)
  63.   {
  64.    return;
  65.   }
  66.   else
  67.   {
  68.    $length = strlen($string)-abs($length)-$start;
  69.    return get_substr($string,$start,$length);
  70.   }
  71. }
  72. else
  73. {
  74.   $start = strlen($string)-abs($start);
  75.   return get_substr($string,$start,$length);
  76. }
  77. }
  78. ?>

[转]SharpDevelop浅析_3_文档编辑器、语法高亮显示

SharpDevelop浅析_3_文档编辑器、语法高亮显示

1、Demo界面及功能解释
启动后,打开文档(默认支持.cs, .js, .java, .aspx等类型文件的语法高亮显示,详见ICSharpCode.TextEditor\Resources\SyntaxModes.xml)、切换语言界面如下:

[阅读全文]

正文提取中用到的正则表达式

 #region 相关正则表达式

 /// <summary>
 /// 去掉所有html标签
 /// </summary>
 private static readonly Regex FilterAll = new Regex(
 @"(\[([^=]*)(=[^\]]*)?\][\s\S]*?\[/\1\])|(?<lj>(?=[^\u4E00-\u9FA5\uFE30-\uFFA0,."");])<a\s+[^>]*>[^<]{2,}</a>(?=[^\u4E00-\u9FA5\uFE30-\uFFA0,."");]))|(?<Style><style[\s\S]+?/style>)|(?<select><select[\s\S]+?/select>)|(?<Script><script[\s\S]*?/script>)|(?<Explein><\!\-\-[\s\S]*?\-\->)|(?<li><li(\s+[^>]+)?>[\s\S]*?/li>)|(?<Html></?\s*[^> ]+(\s*[^=>]+?=['""]?[^""']+?['""]?)*?[^\[<]*>)|(?<Other>&[a-zA-Z]+;)|(?<Other2>\#[a-z0-9]{6})|(?<Space>\s+)|(\&\#\d+\;)",
 RegexOptions.ExplicitCapture
 | RegexOptions.Multiline
 | RegexOptions.IgnoreCase); //(?<Link><a[\s\S]*?</a>)|
 //(?<Style><style[\s\S]+?/style>)|(?<select><select[\s\S]+?/select>)|(?<Script><script[\s\S]*?/script>)|(?<Explein><\!\-\-[\s\S]*?\-\->)|(?<li><li(\s+[^>]+)?>[\s\S]*?/li>)|(?<Html></?\s*[^> ]+(\s*[^=>]+?=['""]?[^""']+?['""]?)*?[^\[<]*>)|(?<Other>&[a-zA-Z]+;)|(?<Other2>\#[a-z0-9]{6})|(?<Space>\s+)

 /// <summary>
 /// 找出title标签
 /// </summary>
 private static readonly Regex FindTitle = new Regex(
 @"<\s*/?title\s*>",
 RegexOptions.ExplicitCapture
 | RegexOptions.Multiline
 | RegexOptions.IgnoreCase);

 /// <summary>
 /// 找出title标签内容
 /// </summary>
 private static readonly Regex FindTitleContent = new Regex(
 @"<\s*/?title\s*>(?<Content>[\s\S]*?)<\s*/?title\s*>",
 RegexOptions.ExplicitCapture
 | RegexOptions.Multiline
 | RegexOptions.IgnoreCase);

 /// <summary>
 /// 找出h 和Strong标签
 /// </summary>
 private static readonly Regex FindHStrong = new Regex(
 @"<\s*/?h\s*>|<\s*/?strong\s*>",
 RegexOptions.ExplicitCapture
 | RegexOptions.Multiline
 | RegexOptions.IgnoreCase);

 /// <summary>
 /// 找出p 和br标签
 /// </summary>
 private static readonly Regex FindPB = new Regex(
 @"<\s*/?p\s*>|<\s*br\s*/?>|<\s*/?tr\s*>",
 RegexOptions.ExplicitCapture
 | RegexOptions.Multiline
 | RegexOptions.IgnoreCase);

 /// <summary>
 /// 找出nbsp标签
 /// </summary>
 private static readonly Regex FindNbsp = new Regex(
 @"&nbsp",
 RegexOptions.ExplicitCapture
 | RegexOptions.Multiline
 | RegexOptions.IgnoreCase);

 /// <summary>
 /// 找出结尾标签
 /// </summary>
 private static readonly Regex FindS = new Regex(
 @"(?<Content>[\s\S]*?)\$",
 RegexOptions.ExplicitCapture
 | RegexOptions.Multiline
 | RegexOptions.IgnoreCase);

 /// <summary>
 /// 找出是否为标准句
 /// </summary>
 private static readonly Regex IsSen = new Regex(
 @"[,.,。!!;;::……??《》“”""]",
 RegexOptions.ExplicitCapture
 | RegexOptions.Multiline
 | RegexOptions.IgnoreCase);

 /// <summary>
 /// 找出是否为垃圾句[strong][h]标签过多的
 /// </summary>
 private static readonly Regex IsWs = new Regex(
 @"\[\(h\)\]",
 RegexOptions.ExplicitCapture
 | RegexOptions.Multiline
 | RegexOptions.IgnoreCase);

 /// <summary>
 /// 找出是否为垃圾句冒号和·-过多的
 /// </summary>
 private static readonly Regex IsWsM = new Regex(
 @"\[·]|[-]|[::]",
 RegexOptions.ExplicitCapture
 | RegexOptions.Multiline
 | RegexOptions.IgnoreCase);

 /// <summary>
 /// 找出是否为BBS特征
 /// </summary>
 private static readonly Regex IsBbsInfo = new Regex(
 @"第[^楼]{1,50}楼|Powered\s*/?by[\s\S]*?Dvbbs|Powered\s*/?by[\s\S]*?Discuz",
 RegexOptions.ExplicitCapture
 | RegexOptions.Multiline
 | RegexOptions.IgnoreCase);
 
 /// <summary>
 /// 取KEYWORD
 /// </summary>
 private static readonly Regex mKeyWord = new Regex(
 @"<meta\s*name\s*=\s*['""]?keywords['""]?\s*content\s*=\s*['""]?(?<KeyWords>[^'"">]*)['""]?[^>]*>|<meta\s*content\s*=\s*['""]?(?<KeyWords>[^'"">]*)['""]?\s*name\s*=\s*['""]?keywords['""]?\s*[^>]*>
",RegexOptions.ExplicitCapture| RegexOptions.Multiline| RegexOptions.IgnoreCase);

 /// <summary>
 /// 取DESCRIPTION
 /// </summary>
 private static readonly Regex mDescription = new Regex(
 @"<meta\s*name\s*=\s*['""]?description['""]?\s*content\s*=\s*['""]?(?<description>[^'"">]*)['""]?[^>]*>|<meta\s*content\s*=\s*['""]?(?<description>[^'"">]*)['""]?\s*name\s*=\s*['""]?description['""]?\s*[^>]*>
",RegexOptions.ExplicitCapture| RegexOptions.Multiline| RegexOptions.IgnoreCase);
 
 /// <summary>
 /// 取Tags
 /// </summary>
 private static readonly Regex mTag = new Regex(
 @"<meta\s*name\s*=\s*['""]?tagwords['""]?\s*content\s*=\s*['""]?(?<tagwords>[^'"">]*)['""]?[^>]*>|<meta\s*content\s*=\s*['""]?(?<tagwords>[^'"">]*)['""]?\s*name\s*=\s*['""]?tagwords['""]?\s*[^>]*>
", RegexOptions.ExplicitCapture | RegexOptions.Multiline | RegexOptions.IgnoreCase);

 /// <summary>
 /// 找出是否为垃圾句:后字符号过少,:号前无“说”字,:号后无"关于"
 /// </summary>
 private static readonly Regex IsWsMM = new Regex(
 @"^[^说\s]{0,8}?[::].{0,10}$",
 RegexOptions.ExplicitCapture
 | RegexOptions.Multiline
 | RegexOptions.IgnoreCase);

 /// <summary>
 /// 找出spider写入的url标记
 /// </summary>
 private static readonly Regex txtUrl = new Regex(
 @"当前URL为:http://(?<URL>.*)",
 RegexOptions.ExplicitCapture
 | RegexOptions.Multiline
 | RegexOptions.IgnoreCase);

 /// <summary>
 /// 找出spider写入的锚点描述标记
 /// </summary>
 private static readonly Regex txtDescription = new Regex(
 @"当前链接描述为:(?<Describe>.*)",
 RegexOptions.ExplicitCapture
 | RegexOptions.Multiline
 | RegexOptions.IgnoreCase);

 ///// <summary>
 ///// 取需要a标签
 ///// </summary>
 //private static readonly Regex cleanFirst = new Regex(
 // @"([\u4E00-\u9FA5]|[\uFE30-\uFFA0]|[,."");])(?<Robbish1><a\s+[^>]*>)[^<]{1,6}(?<Robbish2></a>)([\u4E00-\u9FA5]|[\uFE30-\uFFA0]|[,."");])", RegexOptions.ExplicitCapture | RegexOptions.Multiline | RegexOptions.IgnoreCase);

 #endregion

删除mysql数据库中的重复数据记录

mysql中select distinct * from text不能显示不重复的记录,而是直接全部显示
采用的是下面的方法可删除,假设重复的是test数据库中的title字段
create table bak as (select * from test group by title having count(*)=1);

insert into bak (select * from test group by title having count(*)>1);

truncate table test;

insert into test select * from bak;

稳定在合肥&我们的结婚照

奔波了近两个月,辗转在两个城市,终于稳定下来了,有了家。虽然不大,但很温馨。

火车头的朋友们,到了合肥,直接找我玩哈~~~

奉上我和bb的结婚照:http://locoybb.photo.163.com

*******************************************

今天和bb专门去了趟银行捐点款,还要到宣城路的貌似总行才行,怎么就不开通个绿色通道呢?
数目不多聊表心意,在此为灾区的人民祈祷。

*******************************************

服务器很长时间没打理了,不过好似运行得还不错,没出什么问题,80天了。该远程关下机了~~
attachments/200805/1196479268.jpg

推荐一个折扣网站

http://www.51fanli.com
在当当上买了200多块的书(可以用英文paypal付费~~)

4月底还真给返了12% 30多块咧,够抽几包了~~
·此地為某只的個人YY之地,言論僅代表自己的個人觀點,和現實、社會、政治完全沒關係,沒事請不要在此惹事生非。
·若要轉載本blog内容請註明轉載地址和作者名字,禁止無權轉載/盜鏈等無恥行爲。
·如有轉載侵權請聯系刪除。
·謝謝合作。^_^