C# 使用参数在新线程中运行方法
我有一个asp.net核心控制器,它从视图中获取数据并用它进行搜索 这是控制器代码C# 使用参数在新线程中运行方法,c#,asp.net-core,C#,Asp.net Core,我有一个asp.net核心控制器,它从视图中获取数据并用它进行搜索 这是控制器代码 private readonly GettingWords _repository; public HomeController(GettingWords repository){ _repository = repository; } [HttpPost] public JsonResult SearchWord([FromBody] RequestMode
private readonly GettingWords _repository;
public HomeController(GettingWords repository){
_repository = repository;
}
[HttpPost]
public JsonResult SearchWord([FromBody] RequestModel model){
var result = _repository.GettingWord(model.word, model.adress);
return Json(result);
}
下面是它调用的方法
public class GettingWords
{
public string GettingWord(string word, string adress)
{
string result;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(adress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
{
readStream = new StreamReader(receiveStream);
}
else
{
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
}
string data = readStream.ReadToEnd();
string pattern = word;
// Instantiate the regular expression object.
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
// Match the regular expression pattern against your html data.
Match m = r.Match(data);
if (m.Success)
{
result = "Word " + word + " finded in " + adress;
}
else
{
result = "Word not finded";
}
response.Close();
readStream.Close();
return result;
}
}
我需要在新线程中使用这两个参数运行GettingWord。我如何才能正确地做到这一点
更新
此外,我还需要设置最大线程数,因此我认为仅
任务
并不适合此@Eugene先前关于异步任务的评论是有效的。
如果您需要对任务计数进行更多的控制,您可以在线程池中设置最大数量,或者无论如何更改默认的TaskScheduler(例如,可以找到详细信息)。
这是我不建议做的事情。有人真的需要它,这是非常重要的。看起来在您的情况下,这可能是不正确/不清楚的业务任务。
如果您确信您确实需要为该服务全局设置最大线程数,请考虑使用
@尤金的构建异步流水线。先前关于异步任务的注释是有效的。 如果您需要对任务计数进行更多的控制,您可以在线程池中设置最大数量,或者无论如何更改默认的TaskScheduler(例如,可以找到详细信息)。 这是我不建议做的事情。有人真的需要它,这是非常重要的。看起来在您的情况下,这可能是不正确/不清楚的业务任务。
如果您确信您确实需要为该服务全局设置最大线程数,请考虑使用 的构建异步流水线。
public class GettingWords
{
private static HttpClient _client = new HttpClient();
public async Task<string> GettingWordAsync(string word, string adress)
{
string result;
string data = await _client.GetStringAsync(adress);
string pattern = word;
// Instantiate the regular expression object.
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
// Match the regular expression pattern against your html data.
Match m = r.Match(data);
if (m.Success)
{
result = "Word " + word + " finded in " + adress;
}
else
{
result = "Word not finded";
}
return result;
}
}
公共类获取单词
{
私有静态HttpClient _client=new HttpClient();
公共异步任务GettingWordAsync(字符串字、字符串地址)
{
字符串结果;
字符串数据=等待_client.GetStringAsync(地址);
字符串模式=单词;
//实例化正则表达式对象。
Regex r=新的Regex(模式,RegexOptions.IgnoreCase);
//将正则表达式模式与html数据相匹配。
匹配m=r.匹配(数据);
如果(m.成功)
{
result=“Word”+单词+”在“+地址中找到;
}
其他的
{
结果=“未找到单词”;
}
返回结果;
}
}
然后像这样使用
public class GettingWords
{
private static HttpClient _client = new HttpClient();
public async Task<string> GettingWordAsync(string word, string adress)
{
string result;
string data = await _client.GetStringAsync(adress);
string pattern = word;
// Instantiate the regular expression object.
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
// Match the regular expression pattern against your html data.
Match m = r.Match(data);
if (m.Success)
{
result = "Word " + word + " finded in " + adress;
}
else
{
result = "Word not finded";
}
return result;
}
}
私有只读GettingWords\u存储库
public HomeController(GettingWords repository){
_repository = repository;
}
[HttpPost]
public async Task<JsonResult> SearchWord([FromBody] RequestModel model){
var result = await _repository.GettingWordAsync(model.word, model.adress);
return Json(result);
}
公共HomeController(GettingWords存储库){
_存储库=存储库;
}
[HttpPost]
公共异步任务SearchWord([FromBody]RequestModel){
var result=await_repository.GettingWordAsync(model.word,model.address);
返回Json(结果);
}
您的Getting Words类应该如下所示
public class GettingWords
{
private static HttpClient _client = new HttpClient();
public async Task<string> GettingWordAsync(string word, string adress)
{
string result;
string data = await _client.GetStringAsync(adress);
string pattern = word;
// Instantiate the regular expression object.
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
// Match the regular expression pattern against your html data.
Match m = r.Match(data);
if (m.Success)
{
result = "Word " + word + " finded in " + adress;
}
else
{
result = "Word not finded";
}
return result;
}
}
公共类获取单词
{
私有静态HttpClient _client=new HttpClient();
公共异步任务GettingWordAsync(字符串字、字符串地址)
{
字符串结果;
字符串数据=等待_client.GetStringAsync(地址);
字符串模式=单词;
//实例化正则表达式对象。
Regex r=新的Regex(模式,RegexOptions.IgnoreCase);
//将正则表达式模式与html数据相匹配。
匹配m=r.匹配(数据);
如果(m.成功)
{
result=“Word”+单词+”在“+地址中找到;
}
其他的
{
结果=“未找到单词”;
}
返回结果;
}
}
然后像这样使用
public class GettingWords
{
private static HttpClient _client = new HttpClient();
public async Task<string> GettingWordAsync(string word, string adress)
{
string result;
string data = await _client.GetStringAsync(adress);
string pattern = word;
// Instantiate the regular expression object.
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
// Match the regular expression pattern against your html data.
Match m = r.Match(data);
if (m.Success)
{
result = "Word " + word + " finded in " + adress;
}
else
{
result = "Word not finded";
}
return result;
}
}
私有只读GettingWords\u存储库
public HomeController(GettingWords repository){
_repository = repository;
}
[HttpPost]
public async Task<JsonResult> SearchWord([FromBody] RequestModel model){
var result = await _repository.GettingWordAsync(model.word, model.adress);
return Json(result);
}
公共HomeController(GettingWords存储库){
_存储库=存储库;
}
[HttpPost]
公共异步任务SearchWord([FromBody]RequestModel){
var result=await_repository.GettingWordAsync(model.word,model.address);
返回Json(结果);
}
我能够通过这段代码解决异步问题
public async Task<string> GettingWordAsync(string word, string adress)
{
HttpWebRequest req = WebRequest.CreateHttp(adress);
req.Method = "GET";
req.KeepAlive = true;
string result;
string content = null;
string pattern = word;
HttpStatusCode code = HttpStatusCode.OK;
try
{
using (HttpWebResponse response = (HttpWebResponse)await req.GetResponseAsync())
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
content = await sr.ReadToEndAsync();
}
}
catch (WebException ex)
{
using (HttpWebResponse response = (HttpWebResponse)ex.Response)
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
content = sr.ReadToEnd();
code = response.StatusCode;
}
}
// Instantiate the regular expression object.
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
// Match the regular expression pattern against your html data.
Match m = r.Match(content);
if (m.Success)
{
result = "Word " + word + " finded in " + adress;
}
else
{
result = "Word not finded";
}
return result;
}
}
公共异步任务GettingWordAsync(字符串字、字符串地址)
{
HttpWebRequest req=WebRequest.CreateHttp(地址);
req.Method=“GET”;
req.KeepAlive=true;
字符串结果;
字符串内容=null;
字符串模式=单词;
HttpStatusCode=HttpStatusCode.OK;
尝试
{
使用(HttpWebResponse=(HttpWebResponse)wait req.GetResponseAsync())
{
使用(StreamReader sr=newstreamreader(response.GetResponseStream()))
content=wait sr.ReadToEndAsync();
}
}
捕获(WebException ex)
{
使用(HttpWebResponse=(HttpWebResponse)ex.response)
{
使用(StreamReader sr=newstreamreader(response.GetResponseStream()))
content=sr.ReadToEnd();
代码=response.StatusCode;
}
}
//实例化正则表达式对象。
Regex r=新的Regex(模式,RegexOptions.IgnoreCase);
//将正则表达式模式与html数据相匹配。
匹配m=r.匹配(内容);
如果(m.成功)
{
result=“Word”+单词+”在“+地址中找到;
}
其他的
{
结果=“未找到单词”;
}
返回结果;
}
}
我能够通过这段代码解决异步问题
public async Task<string> GettingWordAsync(string word, string adress)
{
HttpWebRequest req = WebRequest.CreateHttp(adress);
req.Method = "GET";
req.KeepAlive = true;
string result;
string content = null;
string pattern = word;
HttpStatusCode code = HttpStatusCode.OK;
try
{
using (HttpWebResponse response = (HttpWebResponse)await req.GetResponseAsync())
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
content = await sr.ReadToEndAsync();
}
}
catch (WebException ex)
{
using (HttpWebResponse response = (HttpWebResponse)ex.Response)
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
content = sr.ReadToEnd();
code = response.StatusCode;
}
}
// Instantiate the regular expression object.
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
// Match the regular expression pattern against your html data.
Match m = r.Match(content);
if (m.Success)
{
result = "Word " + word + " finded in " + adress;
}
else
{
result = "Word not finded";
}
return result;
}
}
公共异步任务GettingWordAsync(字符串字、字符串地址)
{
HttpWebRequest req=WebRequest.CreateHttp(地址);
req.Method=“GET”;
req.KeepAlive=true;
字符串结果;
字符串内容=null;
字符串模式=单词;
HttpStatusCode=HttpStatusCode.OK;
尝试
{
使用(HttpWebResponse=(HttpWebResponse)wait req.GetResponseAsync())
{
使用(StreamReader sr=newstreamreader(response.GetResponseStream()))
content=wait sr.ReadToEndAsync();
}
}
捕获(WebException ex)
{
使用(HttpWebRes)