How to handle external not stable code?

こ雲淡風輕ζ 提交于 2019-12-12 14:29:44

问题


I have asp.net Web Api 2 Application that is working with External COM Object (pvxcom). In Some reasons COM Object is hanging, there is no chance for me to report bug for pvxcom.

I need to figure out something to bypass this issue. I want to clarify some points.

  1. How can I set maximimum execution time for external source ?
  2. How can I force browser to resent request ? ( is it possible ? )
  3. How can I figure out in which procedure of COM Object is hanging ?
  4. Is it good practice to dispose com object and recreate?

Have you any other ideas, how to figure it out ?


回答1:


You can run it in a thread and you can kill this thread if it hangs or takes longer then expected.

Here is my sketch for MVC, but for WebApi code will be the same. This is based on this answer:

public ActionResult Index()
{
    var sw = new Stopwatch();
    Exception threadException = null;
    var workerThread = new Thread(() =>
    {
        try
        {
            sw.Start();

            // Access your COM here instead of sleep
            Thread.Sleep(6000);

            sw.Stop();
        }
        catch (Exception ex)
        {
            threadException = ex;
            sw.Stop();
        }
    });

    var timeoutTimer = new System.Threading.Timer((s) =>
    {
        workerThread.Abort();
    }, null, 5000, Timeout.Infinite);

    workerThread.Start();
    workerThread.Join();


    ViewBag.Message = String.Format("Time: {0}; Exception: {1}", sw.Elapsed, threadException);

    return View();
}


来源:https://stackoverflow.com/questions/32450160/how-to-handle-external-not-stable-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!