.NET多线程传参数初探

作者:互联网   出处:控件中国网   2014-11-05 19:20:57   阅读:1

控件中国网转载自互联网:.NET多线程传参数初探
 
既然是初探肯定就是刚刚入手不太熟练,本文仅起个抛砖引玉的作用,希望大家能够在本文基础上有所突破,找到一个更好的方法来实现多线程传参问题

线程类:

Code
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using System.IO;
 5
 6namespace threadtest
 7{
 8    class Test
 9    {
10        public int a = 0;
11        public void writeA()
12        {
13            WriteLog(a * 10);
14            for (int i = 0; i <= a; i++)
15            {
16               WriteLog(i);
17            }
18        }
19
20        public void WriteLog(int i)
21        {
22            string file = @"C:\"+a+"testthread.txt";
23            StreamWriter sw = new StreamWriter(file, true);
24            sw.WriteLine(System.DateTime.Now.ToString("hhmmssssss         ") + i.ToString());
25            sw.Flush();
26            sw.Close();
27         
28        }
29    }
30}
31
测试窗体:

 

Code
 1using System;
 2using System.Collections.Generic;
 3using System.ComponentModel;
 4using System.Data;
 5using System.Drawing;
 6using System.Text;
 7using System.Windows.Forms;
 8using System.Collections;
 9
10namespace threadtest
11{
12    public partial class Form1 : Form
13    {
14        public Form1()
15        {
16            InitializeComponent();
17        }
18
19        private void button1_Click(object sender, EventArgs e)
20        {
21            ArrayList tasks = new ArrayList();
22            for (int i = 0; i <= 100; i++)
23            {
24                Test test = new Test();
25                test.a = i;
26                test.WriteLog(i * 1000000);
27                System.Threading.Thread athread = new System.Threading.Thread(new System.Threading.ThreadStart(test.writeA));
28                tasks.Add(athread);
29            }
30            foreach (System.Threading.Thread thread in tasks)
31            {
32                thread.Start();
33            }
34        }
35    }
36}


本文由控件中国网转载
 

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat