具有启动,暂停和停止事件处理的动态线程 [英] Dynamic Threading with Start, Pause and Stop event handling

查看:94
本文介绍了具有启动,暂停和停止事件处理的动态线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I 已创建示例应用程序并实现了线程.基本上旨在使此应用程序普及化是我想

1.如果有任何进程正在运行,则用户界面应通知[DONE]
2.使用ProgressBar [DONE]
处理动态创建的线程 3.从可用进度列表中为启动,暂停和停止线程提供附加功能. [需要您的帮助]

注意:-
我对线程和代理知识不多,所以请让我知道现有代码的最佳解决方案.

使用文件和控件:-
此演示应用程序中基本上使用了三个文件
1. ProgressForm.cs (窗口表单)
其中包含用于创建新进度的Button和Container whic将容纳所有已创建的进度栏
2. ProgressClass.cs
其中包含动态线程和用于通知UI的委托,而无需锁定或挂起用户界面
3. ProgressControl.cs (用户控件)
其中包含


  • 查看屏幕截图
  • 进度栏(以显示已完成的过程)
  • Precent标签(显示已完成进度的百分比)
  • 开始/暂停"按钮(用于播放/暂停线程)
  • 停止"按钮(停止运行线程并从列表中删除进度)
  • StartTime标签(显示进程开始时间)
  • EndTime标签(显示进程完成时间)
  • MaxValue Lable(生成25到100之间的随机数)
  • I have created sample application and implemented threading. basically aim to craete this application is i would like to

    1. If any process(s) are runnig then User Interface should Notify [DONE]
    2. Handle dynamically created thread with ProgressBar [DONE]
    3. Provide addition functionality to Start, Pause and Stop thread from available progress list. [NEED YOUR HELP]

    Note:-
    I don''t have much knowledge about Threading and Delegates, so please let me know best solution for existing code.

    Files and Controls are used:-
    Basically three files are used in this demo application
    1. ProgressForm.cs (Window Form)
    which conatains Button for creating new progress and Container whic will hold all the created progressbars
    2. ProgressClass.cs
    Which contains Dynamic Threading and Delegates to Notify UI without locking or hanging user interface
    3. ProgressControl.cs (User Control)
    Which contains


    • View Screenshot
    • Progressbar (to display process done)
    • Precent Label (display percentage of completed progress)
    • Start/Pause button (for play/pause a thread)
    • Stop button (stop running thread and remove progress from list)
    • StartTime Label (display process started time)
    • EndTime label (display time of process completed)
    • MaxValue Lable (generate random number between 25 to 100)
    • public partial class ProgressForm : Form
          {
              Random randomMaxValue = new Random();
              public ProgressForm()
              {
                  InitializeComponent();
              }
      
              private void btnStart_Click(object sender, EventArgs e)
              {
                   ProgressClass m_clsProcess;
                   ProgressControl progress = new ProgressControl();
                   progress.StartedAt = DateTime.Now;
                   progress.MinValue = 0;
                   progress.CurrentValue = 0;
                   progress.MaxValue = randomMaxValue.Next(25, 100);
                   AddControl(progress);
                   m_clsProcess = new ProgressClass(progress, this, new ProgressClass.NotifyProgress(DelegateProgress));
                   m_clsProcess.Start();
              }
              private void DelegateProgress(int CurrentValue, ProgressControl Progress)
              {
                  ProgressBar p = (ProgressBar)Progress.Controls.Find("pgbPercent", false)[0];
                  p.Minimum = Progress.MinValue;
                  p.Value = CurrentValue;
                  p.Maximum = Progress.MaxValue;
      
                  Label percent = (Label)Progress.Controls.Find("lblPercent", false)[0];
                  percent.Text = string.Format("{0:#00} %", Convert.ToInt16((CurrentValue * 100) / Progress.MaxValue));
      
                  Label start = (Label)Progress.Controls.Find("lblStart", false)[0];
                  start.Text = string.Format("{0:HH:mm:ss}", Progress.StartedAt);
      
                  if (CurrentValue == Progress.MaxValue)
                  {
                      Label complete = (Label)Progress.Controls.Find("lblComplete", false)[0];
                      complete.Text = string.Format("{0:HH:mm:ss}", DateTime.Now);
                      Progress.Status = ProgressControl.ProgressStatus.Completed;
                  }
      
                  Label max = (Label)Progress.Controls.Find("lblMaxValue", false)[0];
                  max.Text = string.Format("{0:#00}", Progress.MaxValue);
      
                  Button btnstartstop = (Button)Progress.Controls.Find("btnStartStop", false)[0];
                  btnstartstop.Click += new EventHandler(ProgressStartStop);
              }
              private void AddControl(Control ctl)
              {
                  tableLayoutPnl.RowCount += 1;
                  tableLayoutPnl.RowStyles.Add(new RowStyle());
                  ctl.Dock = DockStyle.Fill;
                  tableLayoutPnl.Controls.Add(ctl, 0, tableLayoutPnl.RowCount - 1);
              }
              void ProgressStartStop(object sender, EventArgs e)
              {
                  Button btn = sender as Button;
                  //
                  //Here i would like to write a code for START / PAUSE thread and update Image acording too.
                  //
              }
          }



      ProgressControl.cs



      ProgressControl.cs

      public partial class ProgressControl : UserControl
         {
             public enum ProgressStatus
             {
                 Initialize,
                 Running,
                 Paused,
                 Completed
             }
      
             public DateTime StartedAt { get; set; }
             public DateTime CompletedAt { get; set; }
             public int MinValue { get; set; }
             public int CurrentValue { get; set; }
             public int MaxValue { get; set; }
             public ProgressStatus Status { get; set; }
      
             public ProgressControl()
             {
                 InitializeComponent();
                 this.Status = ProgressStatus.Initialize;
             }
         }
      


      ProgressClass.cs


      ProgressClass.cs

      public class ProgressClass
      {
          private int ThreadWaitTime = 100;
          private ProgressControl m_progress;
          private NotifyProgress m_clsNotifyDelegate;
          private System.Threading.Thread m_clsThread;
      
          private System.ComponentModel.ISynchronizeInvoke m_clsSynchronizingObject;
          public delegate void NotifyProgress(int PercentComplete, ProgressControl Progress);
      
          public ProgressClass(ProgressControl Progress, System.ComponentModel.ISynchronizeInvoke SynchronizingObject, NotifyProgress NotifyDelegate)
          {
              m_progress = Progress;
              m_clsSynchronizingObject = SynchronizingObject;
              m_clsNotifyDelegate = NotifyDelegate;
          }
      
          public void Start()
          {
              m_clsThread = new System.Threading.Thread(DoProcess);
              m_clsThread.Name = "Background Thread";
              m_clsThread.IsBackground = true;
              m_progress.Status = ProgressControl.ProgressStatus.Running;
              m_clsThread.Start();
          }
          private void DoProcess()
          {
              for (int i = m_progress.MinValue; i <= m_progress.MaxValue; i++)
              {
                  NotifyUI(i);
                  Thread.Sleep(ThreadWaitTime);
              }
          }
          private void NotifyUI(int Value)
          {
              object[] args = new object[2];
              args[0] = Value;
              args[1] = m_progress;
              m_clsSynchronizingObject.Invoke(m_clsNotifyDelegate, args);
          }
      }



      已更新:
      我不是要编写完整的代码,而是要提供提示.

      我想从列表启动/暂停相关线程,请问我该怎么办?

      我想要以下功能:



      UPDATED:
      I am not asking for write whole code instead of provide hint.

      I would like to start/pause relevent thread from list, os what should i do for that?

      I would like hind in following function:

      void ProgressStartStop(object sender, EventArgs e)
              {
                  Button btn = sender as Button;
                  //
                  //Here i would like to write a code for START / PAUSE thread and update Image acording too.
                  //
              }


      谢谢,
      Imdadhusen


      Thanks,
      Imdadhusen

      推荐答案

      我们不会为您编写代码.尝试自己实施它,如果遇到任何问题,请提问.我现在能为您提供的最佳帮助是google的网址:

      http://www.google.com [ ^ ]
      We''re not going to write the code for you. Try to implement it yourself and ask questions if you experience any problems. The best help I can give you right now is the URL for google:

      http://www.google.com[^]


      这些链接可能会对您有所帮助.
      http://msdn.microsoft.com/zh-CN /library/system.serviceprocess.servicebase(v=vs.80).aspx [ http://msdn.microsoft.com/en-us/library/ck8bc5c6.aspx [ ^ ]

      您可以尝试suspendresume.
      These links might help you.
      http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase(v=vs.80).aspx[^]
      http://msdn.microsoft.com/en-us/library/ck8bc5c6.aspx[^]

      You may try suspend and resume.


      这篇关于具有启动,暂停和停止事件处理的动态线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆