【问题标题】:How to handle external not stable code?如何处理外部不稳定的代码?
【发布时间】:2015-12-03 16:30:24
【问题描述】:

我有 asp.net Web Api 2 与外部 COM Object (pvxcom) 一起使用的应用程序。在 COM 对象挂起的某些原因中,我没有机会报告 pvxcom 的错误。

我需要想办法绕过这个问题。我想澄清一些观点。

  1. 如何设置外部源的最长执行时间?
  2. 如何强制浏览器重新发送请求? (有可能吗?)
  3. 如何判断 COM 对象挂在哪个程序中?
  4. 释放 com 对象并重新创建是一种好习惯吗?

你有什么其他的想法,如何解决?

【问题讨论】:

    标签: c# asp.net com


    【解决方案1】:

    您可以在一个线程中运行它,如果它挂起或花费的时间比预期的要长,您可以终止该线程。

    这是我的 MVC 草图,但对于 WebApi 代码将是相同的。这是基于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();
    }
    

    【讨论】:

    • 谢谢,但我也想得到3,4分的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 2022-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多