【问题标题】:use parametric function in thread in C#在 C# 中的线程中使用参数函数
【发布时间】:2011-03-30 01:09:39
【问题描述】:

你好,这是我的多线程代码,它工作正常

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;

namespace searchIpAdresses
{
    public partial class frmSearchIpRange : Form
    {
        public frmSearchIpRange()
        {
            InitializeComponent();
        }

        int intStartIp, intEndIp;

        private void frmSearchIpRange_Load(object sender, EventArgs e)
        {
            startIp.Text = "0.0.0.0";
            endIp.Text = "0.1.0.0";
            nudThreads.Value = 1;
        }

        private bool calcIpAddressRange()
        {
            intStartIp = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(IPAddress.Parse(startIp.Text).GetAddressBytes(), 0));
            intEndIp = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(IPAddress.Parse(endIp.Text).GetAddressBytes(), 0));
            if (intStartIp>intEndIp)
            {
                MessageBox.Show("End Ip must be bigger than Begin Ip!");
                return false;
            }
            else
            {
                return true;
            }
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            if (calcIpAddressRange())
            {
                int threadCount = Convert.ToInt32(nudThreads.Value);
                Thread[] threads = new Thread[threadCount];              

                for (int i = 0; i < threadCount; i++)
                {
                    threads[i] = new Thread(new ThreadStart(getPerformance));                  
                    threads[i].Name = string.Format(i.ToString());
                }

                foreach (Thread t in threads)
                {
                    t.Start();
                }

                //for (int i = 0; i < nudThreads.Value; i++)
                //{
                //    if (threads[i].IsAlive)
                //        threads[i].Abort();
                //}
            }

        }

        private void getPerformance()//int sampleStartIp, int sampleEndIp
        {
            if (calcIpAddressRange())
            {
                lbAddress.Items.Clear();
                progressBar1.Maximum = intEndIp - intStartIp;
                int i;
                for (i = intStartIp; i < intEndIp; i++)
                {
                    string ipAddress = new IPAddress(BitConverter.GetBytes(IPAddress.NetworkToHostOrder(i))).ToString();

                    progressBar1.Value++;
                    lbAddress.Items.Add(ipAddress);
                }
                MessageBox.Show(i.ToString() + " addresses added", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                progressBar1.Value = 0;
            }
        }
    }
}

但我想在 getPerformance 函数中拆分作业。所以我必须像使用它一样

threads[i] = new Thread(new ThreadStart(getPerformance(sampleStart, sampleEnd)))

等在这种情况下,Visual Studio 给了我“预期的方法名称”错误。解决办法是什么?

编辑:我试过这个,但我无法将它调整为数组格式工作@Henk Holterman

Thread[] threads = new Thread[threadCount];              

                for (int i = 0; i < threadCount; i++)
                {
                    //threads[i] = new Thread(new ThreadStart(getPerformance));                  
                    threads[i].Name = string.Format(i.ToString());

                    Thread t = new Thread(() => getPerformance(intStartIp, intEndIp));
                }

                foreach (Thread t in threads)
                {
                    t.Start();
                }

【问题讨论】:

  • 平台、语言?它看起来像 C#,但要提及它是一个很小的努力。
  • 对不起,我忘记了,现在编辑..
  • getPerformance 将在非 ui 线程上执行,因此对 progressBar1 的更改将不起作用。此外,多个线程可能会同时更改属性,从而使您粘贴的代码容易出现线程问题。

标签: c# multithreading .net-3.5 parameter-passing


【解决方案1】:

1) 您可能应该使用线程池或任务 (Fx4)。

2) 当你可以使用 delegates lamba'a(.NET 3 及更高版本)

  int a=1, b=2;
  Thread t = new Thread(() => Worker(a, b));

对于旧版本,您可以使用ParameterizedThreadStart 和辅助对象来保存开始和结束。


编辑以适应您的循环:

for (int i = 0; i < threadCount; i++)
{
   // some code to update intStartIp, intEndIp 

   //threads[i] = new Thread(new ThreadStart(getPerformance));                  
   threads[i] = new Thread(() => getPerformance(intStartIp, intEndIp));
   threads[i].Name = string.Format(i.ToString());   
}

【讨论】:

  • 我可以将它用于多线程吗?因为这个“t”变量不是数组类型
【解决方案2】:

您可以使用ParameterizedThreadStart 将对象传递给被调用的方法

【讨论】:

    猜你喜欢
    • 2021-01-11
    • 2021-08-03
    • 1970-01-01
    • 2017-10-20
    • 2019-03-24
    • 1970-01-01
    • 2018-09-05
    • 2016-06-27
    • 1970-01-01
    相关资源
    最近更新 更多