参数 1:无法从“方法组”转换;到“ParameterizedThreadStart”
我正在尝试使用另一个 cs 文件中的方法创建一个新线程,但出现此错误。
这是代码
Program.cs:
using HangManSharp;
Backend _backend = new Backend();
Thread initThread = new Thread(_backend.getWord); //Error CS1503 Argument 1: cannot convert from 'method group' to 'ParameterizedThreadStart'
Backend.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HangManSharp
{
internal class Backend
{
public string getWord()
{
const int wordCount = 50000;
int randomWordNum = Random.Shared.Next(wordCount);
try
{
StreamReader sr = new StreamReader("words");
for (int i = 0; i < wordCount; i++)
{
sr.ReadLine();
if (i == randomWordNum)
{
return sr.ReadLine();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Environment.Exit(1);
}
return null;
}
}
}
我想使用多线程,因为我想在获取单词时显示加载屏幕。
我尝试过将所有内容都放在同一个文件中,但它不起作用。
提前致谢
I'm trying to create a new thread using a method from another cs file however I'm getting this error.
Here is the code
Program.cs:
using HangManSharp;
Backend _backend = new Backend();
Thread initThread = new Thread(_backend.getWord); //Error CS1503 Argument 1: cannot convert from 'method group' to 'ParameterizedThreadStart'
Backend.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HangManSharp
{
internal class Backend
{
public string getWord()
{
const int wordCount = 50000;
int randomWordNum = Random.Shared.Next(wordCount);
try
{
StreamReader sr = new StreamReader("words");
for (int i = 0; i < wordCount; i++)
{
sr.ReadLine();
if (i == randomWordNum)
{
return sr.ReadLine();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Environment.Exit(1);
}
return null;
}
}
}
I want to use multithreading because I want to display a loading screen while it gets the word.
I've tried putting everything in the same file and it doesn't work.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论