为了账号安全,请及时绑定邮箱和手机立即绑定

我如何能传入一个引用类型,或者能在函数内部改变的外部变量 , 怎么改?

我如何能传入一个引用类型,或者能在函数内部改变的外部变量 , 怎么改?

饮歌长啸 2022-07-15 17:13:58
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();newThread = new Thread(w.DoMoreWork);// Pass an object containing data for the thread.//newThread.Start("The answer.");}public static void DoWork(object data){Console.WriteLine("Static thread procedure. Data='{0}'",data);}public void DoMoreWork(object data){Console.WriteLine("Instance thread procedure. Data='{0}'",data);}}上面是我找来的一个例子thread函数的参数是 object类型
查看完整描述

3 回答

?
回首忆惘然

TA贡献1847条经验 获得超11个赞

应该不能改吧,我一直用一个比较笨的方法。在类中另开一个元素,然后就不用传参数了。修改的用//----- 加以标注

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();
newThread = new Thread(w.DoMoreWork);

// Pass an object containing data for the thread.
//
newThread.Start("The answer.");
}

public static void DoWork(object data)
{
Console.WriteLine("Static thread procedure. Data='{0}'",
data);
}

public int changeInDoMoreWork = 0; //--------
public void DoMoreWork(object data)
{
changeInDoMoreWork=8; //------------
Console.WriteLine("Instance thread procedure. Data='{0}'",
data);
}
}


查看完整回答
反对 回复 2022-07-18
?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

你的表达我不是太理解,thread的参数可以用委托,委托可以捆绑引用类型的。

查看完整回答
反对 回复 2022-07-18
?
忽然笑

TA贡献1806条经验 获得超5个赞

参数前面加上 ref 吧

查看完整回答
反对 回复 2022-07-18
  • 3 回答
  • 0 关注
  • 97 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信