为什么我不能创建一个使用带参数的方法的线程? [英] Why can I not create a thread that uses a method with arguments?

查看:61
本文介绍了为什么我不能创建一个使用带参数的方法的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试创建一个新的 Thread floodT = new Thread(新的ThreadStart(fn.Flood(fcmd)); 时,它总会给我一个错误告诉我预期的方法名称。



我完全迷失了,我一直在MSDN上阅读有关线程方法的旧存档教程。(线程教程(C#) [ ^ ])我想我一直在努力大概一个小时。我只是卡住了。



请大家帮忙,非常感谢!



提示:所有重要代码都在FloodCmd()方法中启动。



我的代码:

  public   class 命令
{
public voi d 帮助()
{
...其他一些真正代码在这里,但我节省了你的阅读时间。哦,既然你' 重新来这里,我是一位十三岁的程序员,并在Skype上给我留言:facebook:luke.wilson。 1293575 好的,现在我需要获取回到 this ,因为我疯了ADD xc
}

public class 网络
{
public void FloodCmd( String 地址)
{
var fcmd = address.Remove(< span class =code-digit> 0 , 6 ); // 摆脱cmd中的FLOOD,留下地址。

尝试
{
Ping ping = new Ping();
// 关于线程的注意事项:由于某种原因,它们必须完全不同
// master-class。
NetTools fn = new NetTools();
线程floodT = 线程( new ThreadStart(fn.Flood(fcmd))); // 这就是它为我提供语法错误的代码。帮助!

floodT.Start();
ping.Send(fcmd);
}
catch (例外e)
{
System.Diagnostics.Debug.Write( 洪水错误: + e.Message);
Program.WriteError(e.Message);
}
}
}
}

public NetTools
{
公开 无效洪水( 字符串地址)
{
while true
尝试
{
Ping ping = new Ping();

ping.Send(地址);
}
catch (例外e)
{
Program.WriteError(e.Message);
}
}
}





我尝试了什么:



我在互联网上看过教程,我记得这个社区有多好。而且我想把我的困难带给公众,让大家从中学到一点点。

解决方案

首先,你的评论是错误的:没有线程要求在一个不同的班级。你只需要小心你所引用的内容。



这就是DoS攻击程序开始的样子。

不要去那里!

 NetTools fn =  new  NetTools(); 
线程floodT = 线程( new ThreadStart(()= > fn.Flood(fcmd)));
floodT.Start();



是解决这个问题的一种方法。

另一个:

 NetTools fn =  new  NetTools(); 
线程floodT = 线程( new ParameterizedThreadStart(fn.Flood));
floodT.Start(fcmd);



并添加

  public   void  Flood( object 地址)
{
Flood(地址 as string );
}



事实上,你的Flood方法都可以是静态的。

这改变了上面的内容以消除

NetTools fn = new NetTools();

line并更改

fn.Flood

to

NetTools.Flood


我认为你在寻找的是: ParameterizedThreadStart Delegate(System.Threading) [ ^ ]

这里有一个可以作为起点的工作示例。



此链接还包含有关线程的信息:在开始时创建线程并传递数据 [ ^ ]



至于教程,你可以阅读这篇文章: .NET中的线程初学者指南:n的第1部分 [ ^ ]


Every time I attempt to create a new Thread floodT = new Thread(new ThreadStart(fn.Flood(fcmd));, it will always give me an error telling me "Method name expected."

I am completely lost, and I've been on MSDN reading an old archived tutorial about threading methods. (Threading Tutorial (C#)[^]) I think I've been working on this for probably an hour. I'm just stuck.

Please help, and thanks a lot in advance!

HINT: All the important code starts in the FloodCmd() method.

My code:

public class Command
    {
    public void Help()
        {
            ... Some other really really long code goes here, but I saved you reading time. Oh, and since you're here, I'm a thirteen year old programmer, and message me on Skype: facebook:luke.wilson.1293575 Okay, now I need to get back to this, because I have mad ADD xc
        }

        public class Network
        {
            public void FloodCmd(String address)
            {
                var fcmd = address.Remove(0, 6);    // Get rid of "FLOOD " in the cmd, leaving it with the address.

                try
                {
                    Ping ping = new Ping();
                    // NOTE ABOUT THREADS: for some reason they have to be in an entirely different
                    // master-class.
                    NetTools fn = new NetTools();
                    Thread floodT = new Thread(new ThreadStart(fn.Flood(fcmd)));    // This right here is the code it's giving me syntax errors for. HELP!

                    floodT.Start();
                    ping.Send(fcmd);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.Write("Error in flood: " + e.Message);
                    Program.WriteError(e.Message);
                }
            }
        }
    }

    public class NetTools
    {
        public void Flood (String address)
        {
            while (true)
            try
            {
                Ping ping = new Ping();

                ping.Send(address);
            }
            catch (Exception e)
            {
                Program.WriteError(e.Message);
            }
        }
    }



What I have tried:

I've looked all over the internet for tutorials, and I remembered how nice this community is. And I'd like to bring my difficulties to the public for everyone to learn a little bit from.

解决方案

First, your comment is wrong: there's no requirement for threads to be in a different class. You just have to be careful about what you're referencing.

This has the appearance of the beginnings of a DoS attack program.
Don't go there!

NetTools fn = new NetTools();
Thread floodT = new Thread(new ThreadStart(() => fn.Flood(fcmd)));
floodT.Start();


Is one way to solve this.
Another:

NetTools fn = new NetTools();
Thread floodT = new Thread(new ParameterizedThreadStart(fn.Flood));
floodT.Start(fcmd);


and add

public void Flood (object address)
{
  Flood(address as string);
}


In fact, your Flood method(s) can all be static.
This changes the above to eliminate the
NetTools fn = new NetTools();
line and change the
fn.Flood
to
NetTools.Flood


I think what you are looking for is this: ParameterizedThreadStart Delegate (System.Threading)[^]
There is a working example there that you can use as a starting point.

This link also contains information about threads: Creating Threads and Passing Data at Start Time[^]

As for a tutorial you can read this article: Beginners Guide to Threading in .NET: Part 1 of n[^]


这篇关于为什么我不能创建一个使用带参数的方法的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆