日期:2014-05-16  浏览次数:21040 次

多线程 多参数传递问题 新手求帮助
public void  Login(string  ip, int  cmdPort, int  videoPort, int  audioPotrt,  string   Nme,  string  password)
{

 this.strIP = ip;
this.cmdPort  = cmdPort;
thisvideoPort= videoPort;
this.audioPort = audioport

CommonCMDHelper.Instancs.SendCMD("192.168.0.44",  5434 ,ip,speed,0,"CHANNEL_BEST,7000")
Thread.sleep(1000);
}
 我这里的CommonCMD命令需要执行四次 每次间隔1秒 不然设备反应不过来
 放在线程中的话 每次执行程序会假死几秒 表现出来的图像就会卡一下
放到线程里 参数总是传递不对
求大神帮助 在线等。。。。。
------解决方案--------------------
一下代码来自msdn: http://msdn.microsoft.com/en-us/library/1h2f2459(v=vs.110).aspx

下面的例子传了个Int
线程函数可以接受Object类型的参数,把你的参数定义在一个Class中。实例成一个对象,传到线程函数中去。



using System;
using System.Threading;

public class Work
{
    public static void Main()
    {
        // To start a thread using a shared thread procedure, use 
        // the class name and method name when you create the  
        // ParameterizedThreadStart delegate. C# infers the  
        // appropriate delegate creation syntax: 
        //    new ParameterizedThreadStart(Work.DoWork) 
        //
        Thread newThread = new Thread(Work.DoWork);

        // Use the overload of the Start method that has a 
        // parameter of type Object. You can create an object that 
        // contains several pieces of data, or you can pass any  
        // reference type or value type. The following code passes 
        // the integer value 42. 
        //
        newThread.Start(42);

        // To start a thread using an instance method for the thread  
        // procedure, use the instance variable and method name when  
        // you create the ParameterizedThreadStart delegate. C# infers  
        // the appropriate delegate creation syntax: 
        //    new ParameterizedThreadStart(w.DoMoreWork) 
        //
        Work w = new Work();