为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
鸿蒙传说 2021-11-28 19:59:09
我有从 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<>不是很好
查看完整描述

3 回答

?
胡子哥哥

TA贡献1825条经验 获得超6个赞

你的 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;


    }

}

并像这样使用


私人只读 GettingWords _repository;


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);

}


查看完整回答
反对 回复 2021-11-28
?
守着星空守着你

TA贡献1799条经验 获得超8个赞

我能够通过这段代码解决异步问题


  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;

    }

}


查看完整回答
反对 回复 2021-11-28
?
犯罪嫌疑人X

TA贡献2080条经验 获得超4个赞

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


查看完整回答
反对 回复 2021-11-28
  • 3 回答
  • 0 关注
  • 200 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信