在带参数的新线程中运行方法

我有从 View 获取数据并用它进行搜索的 asp.net 核心控制器


这是控制器代码


 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。我怎样才能正确地做到这一点?


更新


另外我需要设置最大线程数,所以我认为这Task<>不是很好


鸿蒙传说
浏览 201回答 3
3回答

胡子哥哥

你的 Getting Words 类应该是这样的public class GettingWords{&nbsp; &nbsp; private static HttpClient _client = new HttpClient();&nbsp; &nbsp; public async Task<string>&nbsp; GettingWordAsync(string word, string adress)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string result;&nbsp; &nbsp; &nbsp; &nbsp; string data = await _client.GetStringAsync(adress);&nbsp; &nbsp; &nbsp; &nbsp; string pattern = word;&nbsp; &nbsp; &nbsp; &nbsp; // Instantiate the regular expression object.&nbsp; &nbsp; &nbsp; &nbsp; Regex r = new Regex(pattern, RegexOptions.IgnoreCase);&nbsp; &nbsp; &nbsp; &nbsp; // Match the regular expression pattern against your html data.&nbsp; &nbsp; &nbsp; &nbsp; Match m = r.Match(data);&nbsp; &nbsp; &nbsp; &nbsp; if (m.Success)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = "Word&nbsp; " + word + "&nbsp; finded in&nbsp; " + adress;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = "Word not finded";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }}并像这样使用私人只读 GettingWords _repository;public HomeController(GettingWords repository){&nbsp; &nbsp; _repository = repository;}[HttpPost]public async Task<JsonResult> SearchWord([FromBody] RequestModel model){&nbsp; &nbsp; var result = await _repository.GettingWordAsync(model.word, model.adress);&nbsp; &nbsp; return Json(result);}

守着星空守着你

我能够通过这段代码解决异步问题&nbsp; public async Task<string> GettingWordAsync(string word, string adress)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; HttpWebRequest req = WebRequest.CreateHttp(adress);&nbsp; &nbsp; &nbsp; &nbsp; req.Method = "GET";&nbsp; &nbsp; &nbsp; &nbsp; req.KeepAlive = true;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; string result;&nbsp; &nbsp; &nbsp; &nbsp; string content = null;&nbsp; &nbsp; &nbsp; &nbsp; string pattern = word;&nbsp; &nbsp; &nbsp; &nbsp; HttpStatusCode code = HttpStatusCode.OK;&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (HttpWebResponse response = (HttpWebResponse)await req.GetResponseAsync())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (StreamReader sr = new StreamReader(response.GetResponseStream()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content = await sr.ReadToEndAsync();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (WebException ex)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (HttpWebResponse response = (HttpWebResponse)ex.Response)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (StreamReader sr = new StreamReader(response.GetResponseStream()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content = sr.ReadToEnd();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; code = response.StatusCode;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Instantiate the regular expression object.&nbsp; &nbsp; &nbsp; &nbsp; Regex r = new Regex(pattern, RegexOptions.IgnoreCase);&nbsp; &nbsp; &nbsp; &nbsp; // Match the regular expression pattern against your html data.&nbsp; &nbsp; &nbsp; &nbsp; Match m = r.Match(content);&nbsp; &nbsp; &nbsp; &nbsp; if (m.Success)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = "Word&nbsp; " + word + "&nbsp; finded in&nbsp; " + adress;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = "Word not finded";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }}

犯罪嫌疑人X

@Eugene 之前关于异步任务的评论是有效的。如果您需要对任务数量进行更多控制,您可以设置线程池中的最大数量或无论如何更改默认 TaskScheduler(例如,可以在此处找到详细信息)。这是我不建议做的事情。有人真正需要它是非常落后的。看起来在您的情况下它可能是不正确/不明确的业务任务。如果您确定确实需要为该服务全局设置最大线程数,请考虑使用TPL 数据流构建异步管道
打开App,查看更多内容
随时随地看视频慕课网APP