gpt4 book ai didi

c# - 在立即窗口中调用C#方法VS,得到 'System.Threading.ThreadAbortException'

转载 作者:行者123 更新时间:2023-11-30 21:42:51 29 4
gpt4 key购买 nike

我正在尝试在 Visual Studio 即时窗口中运行一些相当简单的代码。所有代码所做的就是从文件中读取一些 JSON 输入,并使用它来调用一些其他方法,以将值加载到数据库中。这是代码块:

using Newtonsoft.Json.Linq;
using System;
using System.IO;

namespace POST.API
{
public class Initialization
{
public const string JSON_DATA_FILE = "C:\\OHA_SDD_POST_Development\\POST\\POST.API\\Services\\Setup\\InitializationData.json";
public const string JSON_OBJKEY_DOMAIN = "Domain";
public const string JSON_OBJKEY_ACCOUNTDOMAINTYPE = "AccountDomainType";
public const string JSON_OBJKEY_ORGLOCTYPE = "OrganizationLocationType";

public JObject POSTDataInitJObject;

public JArray Domains;
public JArray AccountDomainRoles;
public JArray OrganizationLocationTypes;

public API.Services.Domain SvcDomain;
public API.Services.Organization SvcOrganization;
public API.Services.Location SvcLocation;
/// <summary>
///
/// </summary>
/// <param name="JsonDataFile"></param>
public Initialization(string JsonDataFile = JSON_DATA_FILE)
{
string JsonData = File.ReadAllText(JsonDataFile);
POSTDataInitJObject = JObject.Parse(JsonData);

Domains = (JArray)POSTDataInitJObject[JSON_OBJKEY_DOMAIN];
AccountDomainRoles = (JArray)POSTDataInitJObject[JSON_OBJKEY_ACCOUNTDOMAINTYPE];
OrganizationLocationTypes = (JArray)POSTDataInitJObject[JSON_OBJKEY_ORGLOCTYPE];
}
/// <summary>
///
/// </summary>
public void Load()
{
LoadDomains();
LoadOrganizationLocationTypes();
}
/// <summary>
///
/// </summary>
/// <param name="Replace"></param>
public void LoadDomains(bool Replace = true)
{
SvcDomain = new API.Services.Domain();

if (Replace)
{
SvcDomain.ClearAllDomains(true);
}

foreach (var i in Domains)
{
SvcDomain.AddDomain(new API.Models.Domain
{
Code = (string)i["Code"],
Definition = new API.Models.TypeDefinition
{
Name = (string)i["Name"],
Description = (string)i["Description"],
Order = Int32.Parse((string)i["Order"])
}
});
}
}
/// <summary>
///
/// </summary>
/// <param name="Replace"></param>
public void LoadOrganizationLocationTypes(bool Replace = true)
{
SvcLocation = new API.Services.Location();

if (Replace)
{
SvcLocation.ClearAllOrganizationLocationTypes();
}

foreach (var i in OrganizationLocationTypes)
{
SvcLocation.AddOrganizationLocationType(new API.Models.OrganizationLocationType
{
Definition = new API.Models.TypeDefinition
{
Name = (string)i["Name"],
Description = (string)i["Description"],
Order = Int32.Parse((string)i["Order"])
}
});
}
}
}
}

我可以在即时窗口中成功实例化该对象,但是当我随后尝试在该实例上调用该 Load() 方法时,我得到:

在 mscorlib.dll 中发生类型为“System.Threading.ThreadAbortException”的第一次机会异常

评估需要线程暂时运行。使用 Watch 窗口执行评估。

我已经关闭了 Options -> Debug -> Enable property evaluation 和其他隐式函数调用。

让我难住了......看起来 super 简单,我完全无法通过它。

最佳答案

你正在直接或间接地调用一些包含 this 的代码:

System.Diagnostics.Debugger.NotifyOfCrossThreadDependency()

为什么代码会有那个?

Some of you might have ran into this situation where you try to evaluate a property or a method during debug time, and get this message in the value cell of the watch:
enter image description here

这个问题有一个快速的“解决方法”。如果仔细看,您会在消息后看到一个小圆形图标: enter image description here

按下此图标将强制继续进行评估,这很可能会给你你想要的结果:
enter image description here

When we break the debugger and try to evaluate a property which requires running code, we can only run code in the current thread. All other threads are frozen to minimize the system state impact… But, what if my property requires another thread to run in order to complete evaluation? For example, if I am doing a remote call, I will most likely need the ThreadPool threads for that… But all other threads are frozen… which will result in a “deadlock”. The debugger has a protection measure against that, which gives a max of 5 seconds to the evaluation code to run. If the code did not finish to run within the time limit, For example, when we are blocked due to a frozen thread, the watch will be “hanged” for 5 seconds, and after that evaluation will be aborted. We will get this message to notify us of the problem: enter image description here

所以那行代码会导致您收到消息:

The debugger will not allow it automatically, to avoid state changes, and instead, will give the user the “The function evaluation requires all threads to run” message.

如果你打开了调试器,那么:

If the user decides it’s OK to let all threads run, he can push the icon next to the message, which will cause the debugger to run the property code a second time, this time with all threads running, and not aborting on the NotifyOfCrossThreadDependency method.

请阅读this文章了解更多详情。

此外,我不确定您在即时窗口中运行所有代码的理由是什么。或许您可以编写一个小型控制台应用程序并改为运行该应用程序。

关于c# - 在立即窗口中调用C#方法VS,得到 'System.Threading.ThreadAbortException',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42214982/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com