先要引用System.ComponentModel

using System.ComponentModel;

然后创建backgroundworker

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            TestArgs args = (TestArgs)e.Argument;
            MyMethod(args.Content, args.Index);
        }

        /// <summary>
        /// backgroundWorker1_DoWork事件使用的参数类
        /// </summary>
        protected class TestArgs
        {
            public string Content { set; get; }
            public int Index { set; get; }

            public TestArgss(string content, int index)
            {
                Content = content;
                Index = index;
            }
        }

        public void BulkFillJson(string content, int index)
        {
            BackgroundWorker backgroundWorker1 = new BackgroundWorker();
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;

            TestArgsargs = new TestArgs(content, index);

            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);

            backgroundWorker1.RunWorkerAsync(args);
        }

 

如果使用.net 4.0并行库中的task,则更简单了,一段就搞定,性能还更高。

            System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                MyMethod(content, index);
            });

 

相关文章:

  • 2021-07-27
  • 2021-12-30
  • 2022-12-23
  • 2021-05-30
  • 2022-12-23
  • 2021-11-17
  • 2021-08-25
  • 2021-06-22
猜你喜欢
  • 2021-06-10
  • 2021-06-12
  • 2022-02-24
  • 2022-01-02
  • 2021-12-29
  • 2022-12-23
相关资源
相似解决方案