【问题标题】:I want to Do Task Parallel in C#.net Winforms Application我想在 C#.net Winforms 应用程序中执行任务并行
【发布时间】:2013-02-27 14:35:39
【问题描述】:

我有 2 个Panels。两个面板都加载给定的时间。现在我想在两个面板上添加一些动画

// animation start of panel1
panel1.show();

// animation start of panel2
panel2.show();

panel1 完成动画后,第二个面板动画开始。 Soo 现在我想同时为两个面板设置动画。

我认为多线程可以做到这一点。 你能告诉我这在我的情况下如何工作吗?

public static class Util { 
    public enum Effect { Roll, Slide, Center, Blend } 
    public static void Animate(Control ctl, Effect effect, int msec, int angle) 
    { 
         int flags = effmap[(int)effect]; 
         if (ctl.Visible) {flags |= 0x10000; angle += 180;} 
         else { 
              if (ctl.TopLevelControl == ctl) flags |= 0x20000; 
              else if (effect == Effect.Blend) throw new ArgumentException(); 
         } 
         flags |= dirmap[(angle % 360) / 45]; 
         bool ok = AnimateWindow(ctl.Handle, msec, flags); 
         if (!ok) throw new Exception("Animation failed"); 
         ctl.Visible = !ctl.Visible; 
     } 

     private static int[] dirmap = { 1, 5, 4, 6, 2, 10, 8, 9 }; 
     private static int[] effmap = { 0, 0x40000, 0x10, 0x80000 }; 

     [DllImport("user32.dll")] private static extern bool 
        AnimateWindow(IntPtr handle, int msec, int flags); 
} 

private void Form1_Load(object sender, EventArgs e) { 
     Util.Animate(radPanel1, Util.Effect.Slide, 700, 360); 
     radPanel1.Show(); 
     expandablePanel1.Expanded = true;
} 

【问题讨论】:

  • 请显示您的动画代码及其实现位置。根据动画的复杂性和性质,可能不需要显式多线程。
  • 您使用的是什么版本的 .Net?如果您使用它,4.0 在线程管理方面有重大升级。
  • 考虑到这两个动画都将在 UI 线程上完成,我认为 Parallel 在这种情况下不会为您提供帮助。
  • 这是我的代码db.tt/jHl5pn2F
  • @HiralBhimani - 我已将您的代码添加到帖子中。这样做是一种很好的做法,而不是将其添加为 Dropbox、pastebin 等的链接。

标签: c# multithreading


【解决方案1】:

我建议使用BackgroundWorkerTimer 组件。

【讨论】:

  • 但如何在我的情况下申请
  • 这些都不适用于对 AnimateWindow 的调用,因为后台线程或计时器最终会编组对 UI 线程的调用,根据设计,UI 线程必须按顺序运行调用. AnimateWindow 不能在单个控件中以任何合理的方式并行化。
猜你喜欢
  • 2021-06-26
  • 2013-06-01
  • 1970-01-01
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
  • 2011-02-24
  • 2013-05-27
  • 1970-01-01
相关资源
最近更新 更多