了解如何调试多线程应用程序 您所在的位置:网站首页 window怎么暂停更新 了解如何调试多线程应用程序

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

2023-05-24 01:41| 来源: 网络整理| 查看: 265

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

适用范围:Visual Studio Visual Studio for Mac Visual Studio Code

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

下面两个主题额外介绍了如何使用其他多线程调试工具:

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

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

首先需要一个多线程应用程序项目。 示例如下。

创建一个多线程应用项目

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

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

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

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

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

注意

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

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

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

新的控制台项目随即显示。 创建该项目后,将显示源文件。 根据所选语言,源文件名称可能是 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 实验室设备网 版权所有