如何处理外部不稳定代码? [英] How to handle external not stable code?

查看:93
本文介绍了如何处理外部不稳定代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 如何为外部源设置最大执行时间?

  2. 如何强制浏览器重新发送请求? (有可能吗?)

  3. 如何确定COM对象挂在哪个过程中?

  4. 处置com对象是否是一种好习惯?并重新创建?

  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 ?

推荐答案

您可以在线程中运行它,如果该线程挂起或花费比预期更长的时间,则可以杀死该线程。

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

这是我的MVC草图,但WebApi的代码相同。这基于此答案

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

这篇关于如何处理外部不稳定代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆