Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果占用UI线程,为什么Windows CE会删除关键事件_C#_Winforms_Compact Framework_Windows Ce - Fatal编程技术网

C# 如果占用UI线程,为什么Windows CE会删除关键事件

C# 如果占用UI线程,为什么Windows CE会删除关键事件,c#,winforms,compact-framework,windows-ce,C#,Winforms,Compact Framework,Windows Ce,现在我明白这个故事的寓意是“不要占用UI线程”,但我们试图通过尽可能长时间地保留UI线程来接吻,但我认为我们刚刚到达临界点,我们将不得不改变我们的设计 但无论如何。。。。。我在我们的设备上注意到了桌面上没有的事情:当你占用UI线程时,它会掉键。下面是一个显示问题的非常简单的应用程序 using System; using System.Windows.Forms; namespace DeviceApplication14 { public partial class Form1 :

现在我明白这个故事的寓意是“不要占用UI线程”,但我们试图通过尽可能长时间地保留UI线程来接吻,但我认为我们刚刚到达临界点,我们将不得不改变我们的设计

但无论如何。。。。。我在我们的设备上注意到了桌面上没有的事情:当你占用UI线程时,它会掉键。下面是一个显示问题的非常简单的应用程序

using System;
using System.Windows.Forms;

namespace DeviceApplication14
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [MTAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private int ctr;

        public Form1()
        {
            InitializeComponent();
            KeyPreview = true;
            KeyDown += Form1_KeyDown;
        }

        void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            for (int i = 0; i < 1000000; i++)
            {

            }
            ctr++;
            button1.Text = ctr.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ctr = 0;
            button1.Text = ctr.ToString();
        }

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(235, 137);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(329, 239);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(62, 291);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(72, 20);
            this.button2.TabIndex = 1;
            this.button2.Text = "Clear";
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
            this.AutoScroll = true;
            this.ClientSize = new System.Drawing.Size(638, 455);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private Button button1;
        private Button button2;
    }
}
使用系统;
使用System.Windows.Forms;
命名空间设备应用程序14
{
公共部分类Form1:Form
{
/// 
///应用程序的主要入口点。
/// 
[MTA线程]
静态void Main()
{
Application.Run(新Form1());
}
私人国际中心;
公共表格1()
{
初始化组件();
KeyPreview=true;
KeyDown+=form1u KeyDown;
}
void Form1\u KeyDown(对象发送方,KeyEventArgs e)
{
对于(int i=0;i<1000000;i++)
{
}
ctr++;
button1.Text=ctr.ToString();
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
ctr=0;
button1.Text=ctr.ToString();
}
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.button1=new System.Windows.Forms.Button();
this.button2=new System.Windows.Forms.Button();
这个.SuspendLayout();
// 
//按钮1
// 
this.button1.Location=新系统图纸点(235137);
this.button1.Name=“button1”;
this.button1.Size=新系统图纸尺寸(329239);
this.button1.TabIndex=0;
this.button1.Text=“button1”;
// 
//按钮2
// 
this.button2.Location=新系统图纸点(62291);
this.button2.Name=“button2”;
this.button2.Size=新系统图纸尺寸(72,20);
this.button2.TabIndex=1;
this.button2.Text=“清除”;
this.button2.Click+=新系统.EventHandler(this.button2\u Click);
// 
//表格1
// 
this.AutoScaleDimensions=新系统.Drawing.SizeF(96F,96F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Dpi;
this.AutoScroll=true;
this.ClientSize=新系统.Drawing.Size(638455);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name=“Form1”;
this.Text=“Form1”;
此选项为.resume布局(false);
}
#端区
私人按钮1;
私人按钮2;
}
}
当我在我的自定义框上运行时,我可以连续按四个键,但我的ctr仅增加两个。如果我再加上几个零(以补偿桌面更快的速度),然后在桌面上运行它,我会得到所有的键

这是怎么回事?

一旦消息进入队列,Windows CE将不会“丢弃”任何密钥。我的猜测是,您使用了太多的quantum,以至于键盘驱动程序本身无法获取所有键,因此无法传递它们。您可以通过调用来验证这一点,而不是使用键盘本身来排除硬件或驱动程序交互。

消息进入队列后,Windows CE不会“丢弃”任何键。我的猜测是,您使用了太多的quantum,以至于键盘驱动程序本身无法获取所有键,因此无法传递它们。您可以通过调用而不是使用键盘本身来排除硬件或驱动程序交互来验证这一点。

for(int i=0;i<1000000;i++)
for (int i = 0; i < 1000000; i++)
            {

            }
{ }
提供时间延迟的空循环表明对事件驱动模型的理解不足

Windows CE有一个与桌面版Windows完全不同的事件处理程序,虽然Windows可以容忍这种类型的滥用,但Windows CE不会

如果您在这样的空循环中占用了大量的处理时间,而不是调用一个延迟例程来正确地将控制释放回调度程序,那么就不会调用事件处理程序。如果没有足够频繁地调用事件处理程序,那么您将丢失事件。存储这些事件的缓冲区多种多样,但根据输入设备(键盘、触摸屏等)的不同,这些缓冲区可能不足或不存在

这里的关键是便携式设备上的事件处理程序没有大的缓冲区。台式计算机有大量的键盘和鼠标操作缓冲区

还有许多其他的区别,但底线是你真的,真的需要适当地释放线程和延迟,而不是这些简单的空循环延迟

在这里,windows CE计时器更合适-使用keydown例程启动计时器,并在它触发时执行所需的操作。这会将控件释放回UI,以便其他事件可以运行

另外,请记住,虽然windows CE是多线程/多任务的,但它是最好的