c# - 应为 C# 方法名称,尝试通过函数调用解析数字
问题描述
我试图调用一个函数“disp”并通过数字 1 进行解析,但有问题指出方法名称是预期的。如果我能理解如何解析一个数字和 threadstart 中的函数,那就太棒了。先感谢您
class Class1
{
public static void disp(int num)
{
try
{
Console.WriteLine(num);
Thread.Sleep(500);
}
catch (Exception e)
{
Console.WriteLine("ERROR");
}
Console.WriteLine("Done");
}
public static void Main(string[] args)
{
ThreadStart ts1 = new ThreadStart(disp(1));
Thread t = new Thread(ts1);
t.Start();
Console.ReadLine();
}
}
}
解决方案
如果要将参数传递给静态方法,则需要使用ParamaterizedThreadStart
class Class1
{
public static void Disp(object num)
{
try
{
Console.WriteLine((int)num);
Thread.Sleep(500);
}
catch (Exception e)
{
Console.WriteLine("ERROR");
}
Console.WriteLine("Done");
}
public static void Main(string[] args)
{
Thread t = new Thread(Class1.Disp);
t.Start(1);
Console.ReadLine();
}
}
}
推荐阅读
- excel - 升级到 O365 后出现“无法共享数据源”
- azure - 如何解决错误消息“查找您的帐户时出现问题。点击下一步重试。” 在 Azure AD 上
- android - 是否可以使用 Android 导航架构组件(Android Jetpack)从“非启动”片段开始?
- office365 - 如何在 Power BI 中使用 python 脚本安排刷新?
- angular - Angular 库和对等依赖项
- google-chrome-devtools - Chrome 扩展 - Devtools - 以编程方式调用元素选择器
- c - 适合快速搜索和简单添加/删除的 C 数据结构
- .net-core - dotnet 发布输出文件夹?
- java - 有没有办法在同一个项目中同时使用 auth0-spring-security-api 和 java-jwt?
- javascript - 如何在 reactJs 中使用 JSON 模式在按下 TAB 键时指向下拉菜单?