我有这个问题,我想通过我的对象类将两个名为Team的自定义类传递给我的Permeterized线程启动类但是我怎么能在这里解决这个问题我的代码
public void Game_Start(object starttime,Team _away,Team _home)
{
string text;
DateTime StartTime = (DateTime)starttime;
Console.WriteLine("Game Starts!");
Show_Left(StartTime);
text = Console.ReadLine();
OnGoal += game_OnGoal;
if (text == "Goal Away") OnGoal.Invoke(_away);
if (text == "Goal Home") OnGoal.Invoke(_home);
}
我无法传递_away和_home参数以及我的主要方法:
static void Main(string[] args)
{
var away = new Team();
var home = new Team();
var counter = new Counter();
Thread work = new Thread(counter.Game_Start);
Timer timer = new Timer(counter.Times_up, work, 5400000, Timeout.Infinite);
DateTime StartTime = DateTime.Now;
work.Start(StartTime);
work.Join();
Console.Write("End");
counter.Show_Left(StartTime);
timer.Dispose();
Console.Read();
}
那么如何才能将两支球队从主队传给我的班级呢?我应该改变什么呢?
答案 0 :(得分:2)
改为使用一个包装类:
public class Teams
{
public Teams(Team home, Team away)
{
Home = home;
Away = away;
}
public Team Home { get; private set; }
public Team Away { get; private set; }
}
答案 1 :(得分:2)
使用lambda表达式:
Thread.Start(()=>Game_Start(starttime, home, away));
注意:这在技术上与@Oded建议的相同,只有包装器类由编译器创建 - 作为闭包。