当前位置:编程学习 > C#/ASP.NET >>

C# 窗口显示问题

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Button Height="23" Margin="78,68,124,0" Name="button1" VerticalAlignment="Top" Click="button1_Click">Button</Button>
        <Ellipse Height="46" HorizontalAlignment="Left" Margin="32,0,0,82" Name="ellipse1" Stroke="Black" VerticalAlignment="Bottom" Width="49" />
        <Ellipse Height="50" Margin="102,0,122,79" Name="ellipse2" Stroke="Black" VerticalAlignment="Bottom" />
        <Ellipse Height="51" HorizontalAlignment="Right" Margin="0,0,42,78" Name="ellipse3" Stroke="Black" VerticalAlignment="Bottom" Width="52" />
    </Grid>
</Window>

      using System.Threading;
       private void button1_Click(object sender, RoutedEventArgs e)
        {
            Thread.Sleep(3000);
            ellipse1.Fill = Brushes.Red;

            Thread.Sleep(3000);
            ellipse2.Fill = Brushes.Red;

            Thread.Sleep(3000);
            ellipse3.Fill = Brushes.Red;
        }

我想问的是,为什么,不是第一个亮了,等三秒,再第二个这,再等三秒,第三个亮,

而是,等九秒过后,一起亮。。。 
--------------------编程问答-------------------- Thread.Sleep(3000);//等3秒
  ellipse1.Fill = Brushes.Red;//1亮

  Thread.Sleep(3000);//又等3秒
  ellipse2.Fill = Brushes.Red;//2亮

  Thread.Sleep(3000);//又等3秒
  ellipse3.Fill = Brushes.Red;//3亮


9秒全亮 
Thread.Sleep(9000);
  ellipse1.Fill = Brushes.Red;
  ellipse2.Fill = Brushes.Red;
  ellipse3.Fill = Brushes.Red;

--------------------编程问答-------------------- using System;
using System.Security.Permissions;
using System.Windows.Threading;

public class DispatcherHelper
{
    /// <summary>
    /// Simulate Application.DoEvents function of <see cref=" System.Windows.Forms.Application"/> class.
    /// </summary>
    [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
    public static void DoEvents()
    {
        var frame = new DispatcherFrame();
        Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
            new DispatcherOperationCallback(ExitFrames), frame);

        try
        {
            Dispatcher.PushFrame(frame);
        }
        catch (InvalidOperationException)
        {
        }
    }

    private static object ExitFrames(object frame)
    {
        ((DispatcherFrame)frame).Continue = false;
        return null;
    }
}

使用:
Thread.Sleep(3000);
ellipse1.Fill = Brushes.Red;
DispatcherHelper.DoEvents();
Thread.Sleep(3000);
ellipse2.Fill = Brushes.Red;
DispatcherHelper.DoEvents();
Thread.Sleep(3000);
ellipse3.Fill = Brushes.Red;
DispatcherHelper.DoEvents();
--------------------编程问答-------------------- 我是想问为什么,而不是想问怎么实现。

 我当然知道怎么做,只要我把ellipse的填充颜色的方法,创建一个线程,那么,只要主线程一停下来,我就可以实现填充了。 --------------------编程问答--------------------
引用 2 楼 caozhy 的回复:
using System;
using System.Security.Permissions;
using System.Windows.Threading;

public class DispatcherHelper
{
  /// <summary>
  /// Simulate Application.DoEvents function of <see cref=" Sys……


还有,这个程序,我不懂它什么意思,看不懂它的机理。看了一下帮助文档,也是不明也以。 --------------------编程问答-------------------- 谢谢2楼,虽然不知道为什么,可是知道以后遇到这种情况怎么做了,但如果可以的话,能帮我讲解一下就更好了。 --------------------编程问答-------------------- Thread.Sleep(3000);当前线程中断3秒
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,