了解如何调试多线程应用程序

您所在的位置:网站首页 vs单步运行 了解如何调试多线程应用程序

了解如何调试多线程应用程序

2024-07-08 05:36| 来源: 网络整理| 查看: 265

开始调试多线程应用程序(C#、Visual Basic、C++) 项目10/25/2023

Visual Studio 提供多种工具和用户界面元素,用于调试多线程应用程序。 本教程演示如何使用线程标记、“并行堆栈” 窗口、“并行监视” 窗口、条件断点、筛选器断点。 完成本教程可使你熟悉用于调试多线程应用程序的 Visual Studio 功能。

下面两篇文章额外介绍了如何使用其他多线程调试工具:

若要使用“调试位置” 工具栏和“线程” 窗口,请参阅演练:调试多线程应用程序。

有关使用 Task(托管代码)和并发运行时 (C++) 的示例,请参阅演练:调试并行应用程序。 有关适用于大多数多线程应用程序类型的常规调试技巧,请阅读该文章和本文章。

第一步是创建多线程应用程序项目。

创建一个多线程应用项目

打开 Visual Studio 并创建一个新项目。

如果“开始”窗口未打开,请选择“文件”>“启动窗口”。

在“开始”窗口上,选择“创建新项目”。

在“创建新项目”窗口的搜索框中输入或键入“控制台” 。 接下来,从“语言”列表中选择“C#”、“C++”或“Visual Basic”,然后从“平台”列表中选择“Windows” 。

应用语言和平台筛选器之后,对 .NET 或 C++ 选择“控制台应用”模板,然后选择“下一步”。

注意

如果没有看到正确的模板,请转到“工具”>“获取工具和功能...”,这会打开 Visual Studio 安装程序 。 选择“.NET 桌面开发”或“使用 C++ 的桌面开发”工作负载,然后选择“修改” 。

在“配置新项目”窗口中,在“项目名称”框中键入或输入 MyThreadWalkthroughApp。 然后,选择“下一步”或“创建”,(视具体提供的选项而定)。

对于 .NET Core 或 .NET 5+ 项目,选择建议的目标框架或 .NET 8,然后选择“创建”。

新的控制台项目随即显示。 创建该项目后,将显示源文件。 根据所选语言,源文件名称可能是 Program.cs、MyThreadWalkthroughApp.cpp 或 Module1.vb 。

删除源文件中显示的代码,并将其替换为以下更新的代码。 为代码配置选择适当的代码片段。

C# VB C++ using System; using System.Threading; public class ServerClass { static int count = 0; // The method that will be called when the thread is started. public void InstanceMethod() { Console.WriteLine( "ServerClass.InstanceMethod is running on another thread."); int data = count++; // Pause for a moment to provide a delay to make // threads more apparent. Thread.Sleep(3000); Console.WriteLine( "The instance method called by the worker thread has ended. " + data); } } public class Simple { public static void Main() { for (int i = 0; i < 10; i++) { CreateThreads(); } } public static void CreateThreads() { ServerClass serverObject = new ServerClass(); Thread InstanceCaller = new Thread(new ThreadStart(serverObject.InstanceMethod)); // Start the thread. InstanceCaller.Start(); Console.WriteLine("The Main() thread calls this after " + "starting the new InstanceCaller thread."); } } Imports System.Threading Public Class ServerClass ' The method that will be called when the thread is started. Public count = 0 Public Sub InstanceMethod() Console.WriteLine( "ServerClass.InstanceMethod is running on another thread.") Dim data = count + 1 ' Pause for a moment to provide a delay to make ' threads more apparent. Thread.Sleep(3000) Console.WriteLine( "The instance method called by the worker thread has ended. " + data) End Sub End Class Public Class Simple Public Shared Sub Main() Dim ts As New ThreadStarter For index = 1 To 10 ts.CreateThreads() Next End Sub End Class Public Class ThreadStarter Public Sub CreateThreads() Dim serverObject As New ServerClass() ' Create the thread object, passing in the ' serverObject.InstanceMethod method using a ' ThreadStart delegate. Dim InstanceCaller As New Thread(AddressOf serverObject.InstanceMethod) ' Start the thread. InstanceCaller.Start() Console.WriteLine("The Main() thread calls this after " _ + "starting the new InstanceCaller thread.") End Sub End Class // #include "pch.h" // Use with pre-compiled header #include #include #include #include int count = 0; void doSomeWork() { std::cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3