- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Thread.Join
方法有三个重载:Join()
, Join(Int32)
和 Join(TimeSpan)
.对于这三个重载中的每一个,都有以下语句 in the Microsoft doc :
If the thread has already terminated when Join is called, the method returns immediately.
Join()
有意义重载,它没有指定为
Join(Int32)
返回哪个值和
Join(TimeSpan)
的,所以我测试了
Int32
在两种不同的环境中过载:
Join
时仍在运行被调用并在调用后终止。它只返回
假 如果线程在调用之前已经终止。
Join
无论平台如何,都应该始终返回 true,那么如何解释这种不一致的行为呢?我错过了什么还是.NET 5 错误?
Starting..
Sleeping 1200..expect T1 end before join
In T1
Leaving T1
Join(100)..expect success
Join(100) success!
Done..
然后,如果我使用
mcr.microsoft.com/dotnet/runtime:5.0 运行此代码在 Docker Desktop (v. 3.1.0) 上,我得到以下结果:
Starting..
Sleeping 1200..expect T1 end before join
In T1
Leaving T1
Join(100)..expect success
Join(100) failed
Done..
更新 2
Join
时才会失败。当 Docker 应用程序正在卸载时(即接收到
AssemblyLoadContext.Default.Unloading
事件,这是 Docker 发送的通知它将关闭应用程序的信号)。
public class Program
{
public static void Main()
{
System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += (arg) => { OnStopSignalReceived("application unloading"); };
}
public static void T1()
{
System.Console.WriteLine("In T1");
System.Threading.Thread.Sleep(1000);
System.Console.WriteLine("Leaving T1");
}
private static void OnStopSignalReceived(string stopSignalSource)
{
System.Threading.Thread t1 = new System.Threading.Thread(T1);
System.Console.WriteLine("Starting..");
t1.Start();
System.Console.WriteLine("Sleeping 1200..expect T1 end before join");
System.Threading.Thread.Sleep(1200);
System.Console.WriteLine("Join(100)..expect success");
if (t1.Join(100))
{
System.Console.WriteLine("Join(100) success!");
}
else
{
System.Console.WriteLine("Join(100) failed");
}
t1.Join();
System.Console.WriteLine("Done..");
}
}
最佳答案
此问题似乎是 AssemblyLoadContext
class 的底层 .NET 5 代码中的错误。或者可能是文档尚未指定的一些未定义行为;也就是说, AssemblyLoadContext.Unloading
event 的文档只是说:
Occurs when the AssemblyLoadContext is unloaded.
AssemblyLoadContext.Unloading
的有趣行为。和线程。
using System;
using System.Diagnostics;
using System.Threading;
using System.Runtime.Loader;
public class Program
{
static Thread t1 = new Thread(ThreadFn);
static Stopwatch sw = new Stopwatch();
public static void Main()
{
AssemblyLoadContext.Default.Unloading += ContextUnloading;
sw.Start();
Console.WriteLine("{0}ms: leaving main", sw.ElapsedMilliseconds);
}
public static void ThreadFn()
{
Console.WriteLine("{0}ms: in ThreadFn, sleeping 1s", sw.ElapsedMilliseconds);
Thread.Sleep(1000);
Console.WriteLine("{0}ms: leaving ThreadFn", sw.ElapsedMilliseconds);
}
private static void ContextUnloading(AssemblyLoadContext context)
{
Console.WriteLine("{0}ms: unloading '{1}', thread state '{2}'", sw.ElapsedMilliseconds, context, t1.ThreadState);
// possible bug/UB with t1.Start() in this function
Console.WriteLine("{0}ms: starting thread", sw.ElapsedMilliseconds);
t1.Start();
Console.WriteLine("{0}ms: calling Sleep(1200); expect thread in state '{1}' to end before join called", sw.ElapsedMilliseconds, t1.ThreadState);
Thread.Sleep(1200);
Console.WriteLine("{0}ms: calling Join(100) on thread in state '{1}'; expect 'succeeded!'", sw.ElapsedMilliseconds, t1.ThreadState);
Console.WriteLine("Join(100) {0}", (t1.Join(100) ? "succeeded!" : "failed"));
Console.WriteLine("{0}ms: done", sw.ElapsedMilliseconds);
}
}
Run it in dotnetfiddle
0ms: leaving main
12ms: unloading '"Default" System.Runtime.Loader.DefaultAssemblyLoadContext #0', thread state 'Unstarted'
13ms: starting thread
14ms: calling Sleep(1200); expect thread in state 'Running' to end before join called
14ms: in ThreadFn, sleeping 1s
1014ms: leaving ThreadFn
1214ms: calling Join(100) on thread in state 'Stopped'; expect 'succeeded!'
Join(100) failed
1214ms: done
您会注意到在这个有缺陷的版本中,每个点的线程状态和时间间隔都与提供的代码相匹配。该错误发生在
Join(Int32)
方法被调用;即使文档说明返回值为
Boolean
其中值为:
true
if the thread has terminated;false
if the thread has not terminated after the amount of time specified by themillisecondsTimeout
parameter has elapsed.
Stopped
,根据
ThreadState
文档,表示线程要么响应了
Abort
调用(在上面的代码中没有调用),或者如果
A thread is terminated.
Be aware of thread races. Loading can be triggered by multiple threads. The AssemblyLoadContext handles thread races by atomically adding assemblies to its cache. The race loser's instance is discarded. In your implementation logic, don't add extra logic that doesn't handle multiple threads properly.
Join(Int32)
应该给出
true
的预期结果在上面的代码中。
Main
函数,而不是在卸载事件处理程序中,
AssemblyLoadContext.Unloading
在
Default
在线程完成并调用
Join(Int32)
之前不会调用上下文。然后,当然,返回预期的结果。
Unloading
是有道理的直到线程完成后才会调用事件,因为它可以被认为是当前程序集上下文的“一部分”,但它没有解释为什么上面代码中的错误仍然发生。
Join(100)
调用确实在下面的代码中按预期成功,这似乎是因为
AssemblyLoadContext.Unloading
在
Main
之后不会被调用如预期的那样退出,而是在线程完成后调用它,这具有上下文意义,但不一定在任何文档中注明。
using System;
using System.Diagnostics;
using System.Threading;
using System.Runtime.Loader;
public class Program
{
static Thread t1 = new Thread(ThreadFn);
static Stopwatch sw = new Stopwatch();
public static void Main()
{
AssemblyLoadContext.Default.Unloading += ContextUnloading;
sw.Start();
// Get expected result starting thread, but Unloading isn't called until AFTER the thread
// finishes, which is not the expected result according to the .NET documentation
Console.WriteLine("{0}ms: starting thread", sw.ElapsedMilliseconds);
t1.Start();
Console.WriteLine("{0}ms: leaving main", sw.ElapsedMilliseconds);
}
public static void ThreadFn()
{
Console.WriteLine("{0}ms: in ThreadFn, sleeping 1s", sw.ElapsedMilliseconds);
Thread.Sleep(1000);
Console.WriteLine("{0}ms: leaving ThreadFn", sw.ElapsedMilliseconds);
}
private static void ContextUnloading(AssemblyLoadContext context)
{
Console.WriteLine("{0}ms: unloading '{1}', thread state '{2}'", sw.ElapsedMilliseconds, context, t1.ThreadState);
Console.WriteLine("{0}ms: calling Sleep(1200); expect thread in state '{1}' to end before join called", sw.ElapsedMilliseconds, t1.ThreadState);
Thread.Sleep(1200);
Console.WriteLine("{0}ms: calling Join(100) on thread in state '{1}'; expect 'succeeded!'", sw.ElapsedMilliseconds, t1.ThreadState);
Console.WriteLine("Join(100) {0}", (t1.Join(100) ? "succeeded!" : "failed"));
Console.WriteLine("{0}ms: done", sw.ElapsedMilliseconds);
}
}
Run it in dotnetfiddle
0ms: starting thread
11ms: leaving main
12ms: in ThreadFn, sleeping 1s
1012ms: leaving ThreadFn
1013ms: unloading '"Default" System.Runtime.Loader.DefaultAssemblyLoadContext #0', thread state 'Stopped'
1014ms: calling Sleep(1200); expect thread in state 'Stopped' to end before join called
2214ms: calling Join(100) on thread in state 'Stopped'; expect 'succeeded!'
Join(100) succeeded!
2215ms: done
您会注意到
Unload
直到线程完成后才会调用事件。
AssemblyLoadContext
class和
346 closed .因此,您的问题可能已经以某种方式被注意到,但粗略的搜索并没有产生任何可能与您的问题相关的内容。
关于c# - 当线程已经终止(.NET 5/Core)时,Thread.Join 方法并不总是返回相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66315427/
主要思想是将 EF Core nuget 包添加到 .NET Core 库项目,然后在一堆应用程序(例如 ASP.NET Core、Win 服务、控制台应用程序)中使用该库,而无需在每个应用程序中配置
我想要实现的是编写一个简单的.net核心后台工作程序(.net core 3.1)的代码,在该工作程序作为Windows服务运行时,我在其中将数据写入SQL Server数据库(通过EF Core 3
关于 .Net Core SDK download page 二进制文件有什么用?它与安装程序有何不同? 最佳答案 二进制文件是 .NET Core 的编译代码。它们拥有运行 .NET Core 所需
.NET Core 和 Entity Framework Core 之间的区别?我们可以在 .NET Core 中使用 Entity Framework Core 吗?两者都有什么优势? 最佳答案 E
.NET Core 和 ASP.NET Core 到底有什么区别? 它们是相互排斥的吗?我听说 ASP.NET Core 是基于 .NET Core 构建的,但它也可以基于完整的 .NET 框架构建。
我对 ASP.NET Core 开发完全陌生。我正在尝试使用单个模型和 mysql 创建一个简单的 asp.net core Web api 来存储模型数据,然后我想使用 Swagger 将其作为 R
.NET Core 和 Entity Framework Core 之间的区别?我们可以在 .NET Core 中使用 Entity Framework Core 吗?两者都有什么优势? 最佳答案 E
好吧,作为一个新的 .net 开发生态系统,我有点迷失在核心工具、版本等方面。 有人可以解释我之间的区别吗 VS 2015 核心工具预览版 x - See here .NET Core/SDK 与否
我已阅读有关如何通过信号器核心集线器从后台服务向客户端发送通知的文档。如何从客户端接收到后台服务的通知? 后台服务应该只是一个单例。 public class Startup { public
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4年前关闭。 Improve t
非常简单的问题: 我正在尝试创建一个像这样的谓词构建器: var predicate = PredicateBuilder.False(); 但似乎在Net Core和EF Core中不可用。
在 .NET Core 自包含应用程序 中...我们需要在 project.json 中指定运行时 (RID) 我们希望我们的应用程序针对...发布为什么会这样? .NET Core 是跨平台的,与我
如何用 iCloud Core Data 替换我现有的 Core Data?这是我的持久商店协调员: lazy var persistentStoreCoordinator: NSPersistent
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 2 年前。 Improv
今天我正在学习新的 ASP.net 核心 API 3.1,我想将我的旧网站从 MVC4 转移到 Web API。除了一件事,一切都很好。数据库连接。在我的旧网站中,我为每个客户端(10/15 数据库)
我在 Visual Studio 2015 Update 3 和 .NET Core 1.0 中工作。我有一个 .NETCoreApp v1.0 类型的 Web API 项目。当我添加一个 .NET
我一直在尝试遵循 Ben Cull ( http://benjii.me/2016/06/entity-framework-core-migrations-for-class-library-proj
当我打开我的 vs 代码程序时,我收到以下消息: 无法找到 .NET Core SDK。 .NET Core 调试将不会启用。确保 .NET Core SDK 已安装并且在路径上。 如果我安装甚至卸载
我偶然发现了一个非常奇怪的问题。每当 Web 应用程序启动时,dotnet.exe 都会使用相当多的内存(大约 300M)。然而,当它触及某些部分时(我感觉这与 EF Core 使用有关),它会在短时
ASP.NET Core Web (.NET Core) 与 ASP.NET Core Web (.NET Framework) 有什么区别? .NET Framework 是否提供 similar
我是一名优秀的程序员,十分优秀!