首页 新闻 会员 周边

(WPF)如下代码导致蓝屏,请问是什么原因?

0
悬赏园豆:5 [待解决问题]

代码如下:但是不是一起来就蓝屏往往 要等会,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace testProcess
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            int num = 0;
            while (true)
            {
                ThreadPool.QueueUserWorkItem(DoProcess, "1");

                new Thread(()=>
                    {
                        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() =>
                            {
                                textBox1.Text = (int.Parse(textBox1.Text) + 1).ToString();
                            }

                            ));
                    }
                    ).Start();
                num++;
                if (num==1000)
                {
                    break;
                }
            }
        

        }

        public void DoProcess(object objec)
        {
            Thread.Sleep(500);
            new Thread(() =>
            {
                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(() =>
                {
                    textBox2.Text = (int.Parse(textBox2.Text) + 1).ToString();
                }

                    ));
            }
                 ).Start();
        }
    }
}

Nelo的主页 Nelo | 初学一级 | 园豆:195
提问于:2010-11-22 16:07
< >
分享
所有回答(2)
0

程序有问题 有死循环 你的机器内存也有问题  可能玩大型游戏的时候也会出现蓝屏

菜鸟老了 | 园豆:145 (初学一级) | 2010-11-22 17:24
没有死循环把。。 if (num==1000) { break; }
支持(0) 反对(0) Nelo | 园豆:195 (初学一级) | 2010-11-23 10:28
可能是临界资源被你的进程或者系统锁住了
支持(0) 反对(0) 菜鸟老了 | 园豆:145 (初学一级) | 2010-11-24 18:36
0

我只是简单的分析下锁死的原因。

 if (num==1000) { break; }这句话远远不可能执行 Why?

对于一个进程来说。最大可以拥有1300个线程。

当num==550 时 你的 Threads count 已经为1136

我这调试时 在num为639时 程序已经使用了1299个线程。

由此推测这段代码造成死机的原因是Threads占用过多。释放过慢过少,申请过多。最后程序将运行缓慢,近乎锁死。

愚溪 | 园豆:250 (菜鸟二级) | 2010-11-29 12:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册