Web Application. Safe to mix QueueUserWorkItem With async-await?


  • Our dev team wrote a non-async method (I'm not allowed to change it to async-await). But I do want to run some code on a thread. So from this sync method, I would like to run something like this:

    ManualResetEvent signalCompletion = new ManualResetEvent(false); oResult result = GetCustomer(custID, signalCompletion);

    ...more code here

    signalCompletion.WaitOne(); Customer customer = result.Customer;

    in reference to this method (it's all set in motion by an Ajax call from client javascript). 

    public oResult GetCustomer(int custID, ManualResetEvent signalCompletion){ //Another sync method. 
        oResult r = new oResult();
        ThreadPool.QueueUserWorkItem(async delegate	{
    	using (Northwind db = new Northwind())
    	  r.Customer = await QueryDbAsync(custID, db);
        return r;
    Does this seem feasible? Or is it raising a lot of red flags?  I'm no expert on threading. 

    • Dipindahkan oleh CoolDadTx Selasa, 12 Juni 2018 19.17 ASP.NET related
    Selasa, 12 Juni 2018 18.36

Semua Balasan