/// <summary> /// ����������״̬�仯�¼� /// </summary> /// <param name="status">������״̬</param> /// <param name="result">�������������</param> protected void FireStatusChangedEvent(TaskStatus status, object result) { if( TaskStatusChanged != null ) { TaskEventArgs args = new TaskEventArgs( status,result); AsyncInvoke(TaskStatusChanged,args); } }
/// <summary> /// ������������ִ���з��ִ����¼� /// </summary> /// <param name="progress">�������(0-100)</param> /// <param name="result">�������м�������</param> protected void FireThrowErrorEvent(int progress, object result) { if( TaskThrowError != null ) { TaskEventArgs args = new TaskEventArgs( progress,result); AsyncInvoke(TaskThrowError,args); } }
/// <summary> /// ������������ȡ���¼� /// </summary> /// <param name="progress">�������(0-100)</param> /// <param name="result">�������м�������</param> protected void FireCancelEvent(int progress, object result) { if( TaskCancel != null ) { TaskEventArgs args = new TaskEventArgs( progress,result); AsyncInvoke(TaskCancel,args); } }
/// <summary> /// ����������ȱ仯�¼� /// </summary> /// <param name="progress">�������(0-100)</param> /// <param name="result">�������м�������</param> protected void FireProgressChangedEvent(int progress, object result) { if( TaskProgressChanged != null ) { TaskEventArgs args = new TaskEventArgs( progress,result); AsyncInvoke(TaskProgressChanged,args); } }
/// <summary> /// ������������ǿ����ֹ�¼� /// </summary> /// <param name="progress">�������(0-100)</param> /// <param name="result">�������м�������</param> protected void FireAbortEvent(int progress, object result) { if( TaskAbort != null ) { TaskEventArgs args = new TaskEventArgs( progress,result); AsyncInvoke(TaskAbort,args); } }
/// <summary> /// �첽���ùҽ��¼�ί�� /// </summary> /// <param name="eventhandler">�¼�����������</param> /// <param name="args">�¼���Ϣ</param> protected void AsyncInvoke(TaskEventHandler eventhandler,TaskEventArgs args) { // TaskEventHandler[] tpcs = (TaskEventHandler[])eventhandler.GetInvocationList(); Delegate[] tpcs = eventhandler.GetInvocationList(); foreach(TaskEventHandler tpc in tpcs) { if ( tpc.Target is System.Windows.Forms.Control ) { Control targetForm = tpc.Target as System.Windows.Forms.Control; targetForm.BeginInvoke( tpc,new object[] { this, args } ); } else { tpc.BeginInvoke(this, args ,null,null); //�첽����,����� } } }
//��UI�߳�,�������״̬��Ϣ�Ͱ�ť״̬ private void OnTaskStatusChanged( object sender, TaskEventArgs e ) { string msg =string.Empty; switch ( e.Status ) { case TaskStatus.Running: button_start.Enabled = false; button_abort.Enabled = true; button_fireerror.Enabled = true; button_stop.Enabled = true; break; case TaskStatus.CancelPending: button_start.Enabled = false; button_abort.Enabled = false; button_fireerror.Enabled = false; button_stop.Enabled = false; break; case TaskStatus.AbortPending: button_start.Enabled = false; button_abort.Enabled = false; button_fireerror.Enabled = false; button_stop.Enabled = false; break; case TaskStatus.Stopped: button_start.Enabled = true; button_abort.Enabled = false; button_fireerror.Enabled = false; button_stop.Enabled = false; label_progress.Text =string.Empty; progressBar.Value =0; break; case TaskStatus.ThrowErrorStoped: button_start.Enabled = true; button_abort.Enabled = false; button_fireerror.Enabled = false; button_stop.Enabled = false; label_progress.Text =string.Empty; progressBar.Value =0; msg =_Task.Exception!=null?_Task.Exception.Message:""; break; case TaskStatus.Aborted: button_start.Enabled = true; button_abort.Enabled = false; button_fireerror.Enabled = false; button_stop.Enabled = false; label_progress.Text =string.Empty; progressBar.Value =0; msg ="��������ǿ����ֹ."; break; } if (_Task.WordThread !=null) label_status.Text = string.Format("����״̬:{0},�߳�����:{1},�̱߳��:{2},�߳�״̬:{3},���н��:{4},������Ϣ:{5}.",e.Status,_Task.WordThread.Name,_Task.WordThread.GetHashCode(),_Task.WordThread.ThreadState,e.Result!=null ? e.Result.ToString():"null",msg); else label_status.Text = string.Format("����״̬:{0},���н��:{1},�����߳�����ΪNULL,������Ϣ:{2}.",e.Status,e.Result!=null ? e.Result.ToString():"null",msg); }
//��UI�߳�,������������ private void OnTaskProgressChanged2( object sender,TaskEventArgs e ) { label_progress.Text = string.Format("����[{0}%],�м���:{1}.",e.Progress.ToString(),e.Result!=null?e.Result.ToString():"null"); }
//��UI�߳�,������½����� private void OnTaskProgressChanged1( object sender,TaskEventArgs e ) { if (InvokeRequired ) //����UI�߳���,�첽���� { TaskEventHandler TPChanged1 = new TaskEventHandler( OnTaskProgressChanged1 ); this.BeginInvoke(TPChanged1,new object[] {sender,e}); Console.WriteLine("InvokeRequired=true"); } else { progressBar.Value = e.Progress; } }