• 大小: 60KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-09
  • 语言: C#
  • 标签: C#  winform  thread  

资源简介

winform多线程与窗体数据交互 C# winform thread

资源截图

代码片段和文件信息

using System;
using System.Threading;
using System.ComponentModel;

namespace WindowsFormsApplication1
{
    public interface IAsyncEventArgs
    {
        int action { get; }
        object[] getobjects { get; }
    }


    /// 
    /// 线程事件参数
    /// 

    internal class AsyncEventArgs : EventArgs IAsyncEventArgs
    {
        private readonly int m_action;
        private readonly object[] m_objects;
        public int action
        {
            get { return this.m_action; }
        }
        public object[] getobjects
        {
            get { return this.m_objects; }
        }

        public AsyncEventArgs(int action object[] objs)
        {
            this.m_action = action;
            this.m_objects = objs;
        }
    }

    /// 
    /// 异步操作基类基类不需要修改,任务应该在子类中完成
    /// 注意:只适用用winform开发
    /// 

    public abstract class Asyncbase
    {

        private Thread m_thread;
        private readonly ISynchronizeInvoke m_Target;
        private bool m_IsStop = false;
        private bool[] m_isCancelFlag = new bool[]{falsefalse};
        protected int m_action = 0;        

        public event EventHandler TaskBefore;
        public event EventHandler TaskProgress;
        public event EventHandler TaskDone;
        public event EventHandler Complete;
        public event EventHandler Error;

        //开始执行任务之前
        private void OnTaskBefore(object sender)
        {
            toTarget(TaskBefore new object[] { sender new AsyncEventArgs(this.m_action null) });
        }

        //执行任务进度
        protected void OnTaskProgress(object sender int step)
        {
            toTarget(TaskProgress new object[] { sender new AsyncEventArgs(this.m_action new object[] { step }) });
        }

        //执行任务成功完成(只有任务成功才会执行,取消和异常均不会执行),传递给主线程的对象在这里设置
        protected void OnTaskDone(object sender object[] objs)
        {
            toTarget(TaskDone new object[] { sender new AsyncEventArgs(this.m_action objs) });
        }

        //执行任务时发生异常
        private void onerror(object sender Exception exp)
        {
            toTarget(Error new object[] { sender new AsyncEventArgs(this.m_action new object[] { exp }) });
        }

        //完成一个任务,无论如果都会执行
        private void OnComplete(object sender)
        {
            toTarget(Complete new object[] { sender new AsyncEventArgs(this.m_action null) });
        }

        /// 
        /// 构造方法
        /// 

        /// 调用目标
        protected Asyncbase(ISynchronizeInvoke target)
        {
            this.m_Target = target;
            this.m_thread = null;
        }


        ~Asyncbase()
        {
            Stop();
        }


        //启动一个工作线程
        public void Start()
        {
            try
            {
                this.m_thread = new Thread(new ThreadStart(Work));
 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2012-12-20 13:48  WindowsFormsApplication1\
     目录           0  2012-12-20 13:48  WindowsFormsApplication1\WindowsFormsApplication1\
     文件         914  2012-12-20 10:45  WindowsFormsApplication1\WindowsFormsApplication1.sln
     文件       24064  2012-12-20 13:47  WindowsFormsApplication1\WindowsFormsApplication1.suo
     文件        6962  2012-12-20 13:37  WindowsFormsApplication1\WindowsFormsApplication1\Async.cs
     文件        2383  2012-12-20 13:45  WindowsFormsApplication1\WindowsFormsApplication1\AsyncImport.cs
     目录           0  2012-12-20 13:48  WindowsFormsApplication1\WindowsFormsApplication1\bin\
     目录           0  2012-12-20 13:48  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\
     文件       14848  2012-12-20 13:46  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe
     文件       38400  2012-12-20 13:46  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.pdb
     文件       11608  2012-12-20 13:46  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe
     文件         490  2010-03-17 22:39  WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe.manifest
     文件        6008  2012-12-20 13:46  WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs
     文件        4788  2012-12-20 13:45  WindowsFormsApplication1\WindowsFormsApplication1\Form1.Designer.cs
     文件        5817  2012-12-20 11:24  WindowsFormsApplication1\WindowsFormsApplication1\Form1.resx
     目录           0  2012-12-20 13:48  WindowsFormsApplication1\WindowsFormsApplication1\obj\
     目录           0  2012-12-20 13:48  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\
     目录           0  2012-12-20 13:48  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\
     文件        6256  2012-12-20 13:46  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         686  2012-12-20 13:46  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\GenerateResource-ResGen.read.1.tlog
     文件        1222  2012-12-20 13:46  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\GenerateResource-ResGen.write.1.tlog
     目录           0  2012-12-20 10:45  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\TempPE\
     文件        1443  2012-12-20 13:46  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\WindowsFormsApplication1.csproj.FileListAbsolute.txt
     文件       14848  2012-12-20 13:46  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\WindowsFormsApplication1.exe
     文件         180  2012-12-20 13:46  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\WindowsFormsApplication1.Form1.resources
     文件       38400  2012-12-20 13:46  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\WindowsFormsApplication1.pdb
     文件         180  2012-12-20 13:46  WindowsFormsApplication1\WindowsFormsApplication1\obj\x86\Debug\WindowsFormsApplication1.Properties.Resources.resources
     文件         505  2012-12-20 10:45  WindowsFormsApplication1\WindowsFormsApplication1\Program.cs
     目录           0  2012-12-20 13:48  WindowsFormsApplication1\WindowsFormsApplication1\Properties\
     文件        1380  2012-12-20 10:45  WindowsFormsApplication1\WindowsFormsApplication1\Properties\AssemblyInfo.cs
     文件        2896  2012-12-20 10:45  WindowsFormsApplication1\WindowsFormsApplication1\Properties\Resources.Designer.cs
............此处省略5个文件信息

评论

共有 条评论