C#中的多线程实现和注意事项

using System.Threading.Tasks;
using System.Threading;
using System.Net.Sockets;
using System.Net;


==============================================================================
 Task t = new Task(() =>
     {
        //To Do you code  也可以在这直接调用方法,直接传递参数,也比较方便
     });

t.Start();
================================================================================

listBox1.Items.Clear();           //清空
===================================================================================
//用内置委托Action实现UI组件刷新

 Action<int> action = (data) =>                    //INT可以改为String等其他格式,根据你要显示的内容灵活组合
                {
                listBox1.Items.Add(data);
                };
                Invoke(action, i);



//委托同步执行,Action 与 Func是.NET类库中增加的内置委托,以便更加简洁方便的使用委托。

内置委托类型,顾名思义Action和Func本身就是已经定义好的委托类型。两种委托类型的区别在于:Action委托签名不提供返回类型,而Func提供返回类型。

Action委托具有Action<T>、Action<T1,T2>、Action<T1,T2,T3>……Action<T1,……T16>多达16个的重载,其中传入参数均采用泛型中的类型参数T,涵盖了几乎所有可能存在的无返回值的委托类型。
Func则具有Func<TResult>、Func<T,Tresult>、Func<T1,T2,T3……,Tresult>17种类型重载,T1……T16为出入参数,Tresult为返回类型。
===================================================================================
Thread thread = new Thread(new ThreadStart(Test));
thread.IsBackground = true;      //设置为后台线程      
thread.Start();

//这个方法也可以启动多线程

====================================================================================
实例:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net;

namespace TASK
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

           // Control.CheckForIllegalCrossThreadCalls = false;//关闭安全检查,为了安全一般不采用此代码
        }
        
        private void button1_Click(object sender, EventArgs e)
        {

            listBox1.Items.Clear();  //清空原数据
            Task t = new Task(() =>
            {
                saomiao();                                      
            });
            t.Start();
        }
        private void saomiao()
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = IPAddress.Parse(textBox1.Text);
            int port, port1, port2;

            port1 = Convert.ToInt32(textBox2.Text);
            port2 = Convert.ToInt32(textBox3.Text);

            for (port = port1; port <= port2; port++)                         //端口循环扫描控制
            {
                try
                {
                    sock.Connect(new IPEndPoint(ip, port));                   //链接
                    //this.SetText(port.ToString() + "   号端口连接服务器成功");
                    Action<String> ac = (data) =>
                    {
                        listBox1.Items.Add(data);
                    };
                    Invoke(ac, port.ToString() + "    号端口连接服务器失败");
                    //resuat1.Items.Add(port.ToString() + "   号端口连接服务器成功");
                }
                catch
                {
                    //this.SetText(port.ToString() + "   号端口连接服务器失败");
                    //resuat1.Items.Add(port.ToString() + "    号端口连接服务器失败");
                    Action<String> ac = (data) =>
                    {
                        listBox1.Items.Add(data);
                    };
                    Invoke(ac, port.ToString() + "    号端口连接服务器失败");
                }
            }
            //resuat1.Items.Add("扫描结束......");
            Action<String> action = (data) =>
            {
                listBox1.Items.Add(data);
            };
            Invoke(action, "扫描完毕......");
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}

  

猜你喜欢

转载自www.cnblogs.com/gougouwang/p/12791239.html