首页 \ 问答 \ ToArray()会抛出异常吗?(Can ToArray() throw an exception?)

ToArray()会抛出异常吗?(Can ToArray() throw an exception?)

虽然这个问题的答案非常好,但它意味着您应该将对List.ToArray()的调用环绕在并发锁中。 这篇博文也暗示它可能会发生灾难性的失败(但很少)。 在枚举List或其他集合时,我通常使用ToArray而不是锁定,以避免“Collection Modified,Enumeration may not complete”异常。 这个答案和博客文章已经将这个假设称为问题。

List.ToArray()的文档没有列出任何异常,所以我一直认为它会一直完成(尽管可能是陈旧的数据),并且从数据一致性的角度来看它不是线程安全的,但它是线程从代码执行的角度来看是安全的 - 换句话说,它不会抛出异常并调用它不会破坏底层集合的内部数据结构。

如果这种假设不正确,那么它从来没有引起过问题,但它可能是高可用性应用程序中的时间炸弹。 什么是明确的答案?


While the answer to this question is excellent, it implies that you should surround calls to List.ToArray() in a lock for concurrency. this blog post also implies that it could fail catastrophically (but rarely). I typically use ToArray rather than a lock in when enumerating Lists or other collections in order to avoid the "Collection Modified, Enumeration may not complete" exception. This answer and the blog post have called that assumption into question.

The documentation for List.ToArray() does not list any exceptions, so I have always assumed that it will always complete (albeit maybe with stale data) and that while it is not thread safe from a data consistency point of view, it is thread safe from a code execution point of view - in other words, it won't throw an exception and calling it won't corrupt the internal data structure of the underlying collection.

If this assumption isn't correct, then while it has never caused a problem it could be a timebomb in a high availability application. What is the definitive answer?


原文:https://stackoverflow.com/questions/14946044
更新时间:2023-08-03 13:08

最满意答案

你可以使用其中一个本机JS ...

window.onload = function() {
    // your code here
};

还是jQuery ......

$(document).ready(function() {
    // your code here
});

...确保代码在文档加载完成之前不会运行。

这解释了window.onload和$(document).ready()之间的细微差别。

另一个选择是将代码包装在一个命名函数中并在某个地方调用它,但你仍然需要将它放在<script>标记中。

编辑:使用window.onload ...

<html>
<head>
<script>
window.onload = function() {
    new TWTR.Widget({
              version: 2,
              type: 'faves',
              rpp: 1,
              interval: 7200000,
              title: '',
              subject: '',
              width: 500,
              height: 65,
              theme: {
                shell: {
                  background: '#a4c9b9',
                  color: '#ffffff'
                },
                tweets: {
                  background: '#a4c9b9',
                  color: '#ffffff',
                  links: '#444444'
                }
              },
              features: {
                scrollbar: true,
                loop: false,
                live: false,
                behavior: 'all'
              }
            }).render().setUser('exampleuser').start();
};
</script>
</head>
<body></body></html>

You can use one of either native JS...

window.onload = function() {
    // your code here
};

or jQuery...

$(document).ready(function() {
    // your code here
});

...to ensure the code will not run until the document has finished loading.

This explains the slight difference between window.onload and $(document).ready().

Another option would be to wrap your code in a named function and call it in the body somewhere but you would still have to put it in <script> tags.

EDIT: Using window.onload...

<html>
<head>
<script>
window.onload = function() {
    new TWTR.Widget({
              version: 2,
              type: 'faves',
              rpp: 1,
              interval: 7200000,
              title: '',
              subject: '',
              width: 500,
              height: 65,
              theme: {
                shell: {
                  background: '#a4c9b9',
                  color: '#ffffff'
                },
                tweets: {
                  background: '#a4c9b9',
                  color: '#ffffff',
                  links: '#444444'
                }
              },
              features: {
                scrollbar: true,
                loop: false,
                live: false,
                behavior: 'all'
              }
            }).render().setUser('exampleuser').start();
};
</script>
</head>
<body></body></html>

相关问答

更多
  • 你需要添加这个: html { background: transparent } 或者 ,在html上设置“页面背景”( background: black ),这很好。 为什么? 在Bootstrap里面,有这样的: html,body{background-color:#ffffff;} (请记住默认background值是transparent ) 有关为什么这很重要的更多信息,请参阅: 将CSS规则应用于html与body相比有什么区别? 请注意,这不再是Bootstrap 3+的问题。 Yo ...
  • 你可以用正则表达式提取它。 类似于:/ /\]*\>(.*)\<\/body/m ^ /\]*\>(.*)\<\/body/m .*) /\]*\>(.*)\<\/body/m /\]*\>(.*)\<\/body/m - 应该返回元素中的所有内容。 $val = getData('myWebsite.html'); var reg = /\]*\>([^]*) ...
  • 你在popover元素标签中遗漏了一些东西,格式是这样的: Click Me 然后它应该工作,例如: http : //jsfiddle.net/52VtD/1983/ You are missing some things in your popover element tag, have the format be something like this:
  • 你可以使用其中一个本机JS ... window.onload = function() { // your code here }; 还是jQuery ...... $(document).ready(function() { // your code here }); ...确保代码在文档加载完成之前不会运行。 这解释了window.onload和$(document).ready()之间的细微差别。 另一个选择是将代码包装在一个命名函数中并在某个地方调用它,但你仍然需要将它放在
  • HTML规范绝对允许省略html , head和body 标签 。 基本原因是浏览器一直设法与现有网页保持一致,并且HTML的早期版本没有定义这些元素。 当HTML 2.0 首先,它的做法是在标签丢失时被推断。 我经常发现在原型设计时省略标签是很方便的,特别是在编写测试用例时,有助于将标记重点放在有问题的测试中。 推理过程应该按照您在Firebug中看到的方式创建元素,并且浏览器在执行此操作时非常一致。 但... IE在这方面至少有一个已知的bug。 即使IE9表现出来。 假设标记是这样的:
  • 我注意到代码中有两件事需要解决: 在您将高度列为height:"600px" ,这是无效的。 您的示例中缺少以下CSS。 #twitter-widget-holder { max-height:400px; max-width:320px; padding:20px; background:#fff; border-radius:3px; box-shadow:2px 2px 3px rgba(0,0,0,.1); } 我继续前进,并进行 ...
  • 五个星期前,Twitter退出旧的v1 API,所以你应该使用新的。