C#從零開始_自學C#、Visual Studio實境秀 51-52/ 偵錯功能及介面



52/{ 偵錯功能及介面

23:00 使用偵錯工具巡覽程式碼 Navigate Code with the Visual Studio Debugger {http://bit.ly/2vYwdxb{

Run to a function specified by name

在呼叫堆疊視窗中指定要偵錯的函式:You can tell the debugger to run your application until it reaches a specified function. You can specify the function by name or you can choose it from the call stack.

To specify a function by name, choose Debug, New Breakpoint, Break at Function, then enter the name of the function and other identifying information.

}

29:50 Use Breakpoints in the Visual Studio Debugger{http://bit.ly/2vwnQvL

42:40 View the call stack and use the Call Stack Window in the Visual Studio debugger{http://bit.ly/2xCc80l

51:00 Display non-user code in the Call Stack window

Right-click the Call Stack window and select Show External Code. 在呼叫堆疊視窗中顯示外部程式碼

Non-user code is any code that is not shown when Just My Code is enabled. In managed code, non-user code frames are hidden by default. The following notation appears instead of the non-user code frames:

[<External Code>] [外部程式碼]

57:00 Switch to another stack frame (change the debugger context)

debugger context 偵錯工具內容

Switch to Frame 切換至框架

Go To Source Code 移至原始程式碼

Run to Cursor 執行至游標處

Include Calls To/From Other Threads. 包含至/從其他執行緒的呼叫

1:08:00  Load Symbols for a module

}

1:11:24 but you cannot set a breakpoint on the namespace or class declarations or the method signature.

1:12:40 中斷點的種類 Setting Other Kinds of Breakpoints

line breakpoint

1:20:00 利用中斷點視窗管理中斷點 Managing Breakpoints

You can use the Breakpoints window (Debug > Windows > Breakpoints, or CTRL + ALT + B) to see all the breakpoints you have set in your solution:

還可以把中斷點儲存或匯出 If you need to save or share the state and location of a set of breakpoints, you can export and import breakpoints only from the Breakpoints window.

Advanced Breakpoints

Breakpoint conditions

中斷點設定 Right-click the breakpoint, or hover over the breakpoint and choose the settings icon.

Breakpoint Settings window

Conditional Expression 條件運算式

可以設定中斷點中斷的時機

1:34:10 在中斷點的條件句裡頭使用物件ID:Using Object IDs in Breakpoint Conditions (C# and F#)

In C# and F#, you can create object IDs for specific instances of reference types and use them in breakpoint conditions. The object ID is generated by the common language runtime (CLR) debugging services and associated with the object. To create an object ID, do the following:

Make Object ID 使用物件ID

You should see a $ plus a number in the Locals window. This is the object ID.

1:39:00 刪除物件ID If you later want to delete the object ID, you can right-click the variable in the Locals window and select Delete Object ID.

Hit Count 叫用次數

1:42:10 可以用中斷點被觸擊(hit)了幾次來決定中斷點是否要被啟用:If you suspect that a loop in your code starts misbehaving after a certain number of iterations, you can set a breakpoint to stop execution after a specified number of hits to the to the associated line of code, rather than being forced to repeatedly press F5 to reach the iteration level.

In the Breakpoint Settings window, set the condition to Hit Count. You can then specify the number of iterations. In the following example, we set the breakpoint to hit on every other iteration:

1:46:10 Filter

還可以指定中斷點只在某個裝置、程序或線程(執行緒)叫用時才被觸發:You can restrict a breakpoint to fire only on specified devices, or in specified processes and threads.

In the Breakpoint Settings window, set the condition to Filter. Enter one or more of the following expressions.

MachineName = "name"

ProcessId = value

ProcessName = "name"

ThreadId = value

ThreadName = "name"

Enclose string values in double quotes. You can combine clauses using & (AND), || (OR), ! (NOT), and parentheses.

中斷點設定視窗裡的「動作」 Breakpoint Actions and Tracepoints

A tracepoint is a breakpoint that prints a message to the Output window.

2:03:00 中斷點標籤 Breakpoint labels

Breakpoint labels are used only in the Breakpoints window to sort and filter the list of breakpoints. To add a label to a breakpoint, choose the breakpoint row and then choose Label on the context menu.

2:05:30 匯出匯入中斷點 Export and Import Breakpoints

在方案目錄(資料夾)下會儲存匯出的中斷點資訊:You can export a breakpoint to an XML file by right-clicking on the breakpoint and selecting Export. The file is saved by default in the solution directory.

中斷點的疑難排除 :

I deleted a breakpoint, but I continue to hit it when I start debugging again

If you deleted a breakpoint while debugging, in some cases you may hit the breakpoint again the next time you start debugging. To stop hitting this breakpoint, make sure all the instances of the breakpoint are removed from the Breakpoints window.

}

Learn Productivity Tips and Tricks for the Debugger in Visual Studio{http://bit.ly/2wsUNGU



}

}

51/{ 當機導致放棄,今併入52發表,以便檢索



3:58:00 更新 Virtual Box 4:17:00 更新成功

25:00 設定物件ID 再練習、測試

27:00 Design Patterns: Decorator {https://youtu.be/6PPMR0GWrZQ

I…… interface

This pattern allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.

43:10 Design Patterns: Adapter and Façade{https://youtu.be/XYa0rmRMZ1Q



}

}



1;36:30 Feature Tour of the Visual Studio Debugger 偵錯基礎 偵錯入門 {http://bit.ly/2xFWY9n

只偵錯使用者寫的程式碼 By default, the debugger skips over non-user code (if you want more details, see Just My Code).

Step into a property

Step Into Specific 逐步執行至指定處

Run to Click 綠色的小按鈕 2:07:00 Using this button is similar to setting a temporary breakpoint.

2:27:30 可以一邊編輯一邊偵錯(可以在偵錯時編輯程式碼):In most supported languages, you can edit code in the middle of a debugging session. For more info, see Edit and Continue.

2:29:40 Edit code and continue Debugging in Visual Studio (C#, VB, C++){http://bit.ly/2iCRJ7B

How to: Enable and Disable Edit and Continue (C#, VB, C++){http://bit.ly/2vCK1vz

Tools > Options > Debugging

Select General, and scroll down to Edit and Continue category (right pane).

Enable Edit and Continue 啟用編輯後繼續

IntelliTrace

Enable Native Edit and Continue 啟用原生編輯後繼續

Apply changes on continue (Native only) 繼續時套用變更(僅限機器碼)

}

Edit and Continue (Visual C#){http://bit.ly/2xqqP6n

2:41:00 要在中斷模式(in break mode )中才行:With Edit and Continue for C#, you can make changes to your code in break mode while debugging. The changes can be applied without having to stop and restart the debugging session. In run mode, the source editor is read-only.

Edit and Continue supports most changes you might want to make during a debugging session, but there are some exceptions. For more information, see Supported Code Changes (C#).

How to: Use Edit and Continue (C#){http://bit.ly/2vw65Jg

2:43:50 2:46:30 Some changes are not supported by Edit and Continue. For more information, see Supported Code Changes (C#).

}

Supported Code Changes (C#){http://bit.ly/2wi2vUf

2:50:10 The current statement is marked by a yellow background in the source window. Other active statements are marked by a shaded background and are read-only. These default colors can be changed in the Options dialog box.

2:54:40 Editing code after an unhandled exception, when the "Unwind the call stack on unhandled exceptions" option is not selected.

}

}

}

2:59:20 在自動變數視窗和區域變數視窗中監看變數值:Inspect variables with the Autos and Locals windows

3:01:00 自動變數視窗是監看目前行和前一行的變數:The Autos window shows all variables used on the current line or the preceding line

3:05:10 The Locals window shows you the variables that are currently in scope. 就是在結界(有效範圍)中的區域變數

3:08:00 Set a watch Watch window 監看視窗 you can see its value change as you move through the debugger. Unlike the other variable windows, the Watch windows always show the variables that you are watching (they're grayed out when out of scope).

For more info, see Set a Watch using the Watch and QuickWatch Windows http://bit.ly/2xGcUIG

3:10:30 Examine the call stack 呼叫堆疊

The Call Stack window shows the order in which methods and functions are getting called. The top line shows the current function

The call stack is a good way to examine and understand the execution flow of an app.

The Call Stack window is similar to the Debug perspective in some IDEs like Eclipse.

You can double-click a line of code to go look at that source code and that also changes the current scope being inspected by the debugger. This does not advance the debugger.

You can also use right-click menus from the Call Stack window to do other things. For example, you can insert breakpoints into specific functions, restart your app using Run to Cursor, and to go examine source code. See How to: Examine the Call Stack.

3:19:50 對例外情形的處理:Examine an exception

When your app throws an exception, the debugger takes you to the line of code that threw the exception.

 Exception Helper

 The Exception Helper replaces the Exception Assistant in Visual Studio 2017.

3:27:00 多執行緒(多線程)的偵錯:Debug Multithreaded Applications Describes how to debug multithreaded applications.

3:30:00 偵錯的竅門、訣竅 Debugger Tips and Tricks Learn how to increase your productivity with the debugger.

}



3:41:40 What's New for the Debugger in Visual Studio 2017{http://bit.ly/2vwvN04

Exception Helper

3:46:00 Run execution to here 小綠色箭頭按鈕  green arrow icon

This eliminates the need to set temporary breakpoints.

3:48:20 You can set conditions on exceptions in the Exception Settings dialog box (You can do this by using the Edit condition icon in the Exception Settings dialog box or by using the right-click menu on the exception.)

3:49:10 Attach to Process dialog box includes a new search feature that can help you more quickly identify the process that you need to attach to.



3:45:00 Using the New Exception Helper in Visual Studio 2017{http://bit.ly/2glAC9J

4:35:00 In previous versions of Visual Studio, when you break on an exception, you will see the Exception Assistant (when debugging managed code), or the Exception Dialog (when debugging Native code, or if you disabled the Exception Assistant).

We looked closely at both of these dialogs, listened to your feedback on User Voice and from Send-A-Smile, and decided they needed an overhaul.

4:37:40 Highlights of the New Exception Helper

Breaking on the Line of Code – No matter if you are managed or native debugging, when you break on an exception the debugger will stop you on the line of code where that exception happens. An exception error icon will appear to the right of that line of code and the non-modal, smaller, less distracting Exception helper will pop down from that icon and stay pinned to that code line.

Null值的除錯檢查:Null Reference Analysis – Starting in VS 2017 , for both managed and native code, when you hit a NullReferenceException or an Access Violation, you will see null analysis information in the Exception Helper. If we cannot determine what is null we will not attempt to show any analysis. When we can detect what is null we will emphasize the culprit in bold text. For C++ Access Violations, this analysis will be present in Visual Studio 2015 as well.

Note: Null reference analysis in managed code requires .NET version 4.6.2. Null analysis is currently not supported for Universal Windows Platform (UWP) and any other .NET Core applications. It is only available while debugging code that does not have any Just-In-Time (JIT) code optimizations.

4:44:00 Configure your Exception Settings – When you break on Exception Thrown you can use the checkbox under Exception Settings in the Exception Helper to disable breaking on that exception type when thrown in the future. You can also specify that you do not want to break on this particular exception thrown in this particular module by checking the box by the module name under “Except when thrown from:” in the Exception Settings. You can apply conditions to your exceptions to only break when thrown from certain modules.

Move it where you need it – You can drag the Exception Helper from its pinned view to its floating view if it happens to cover up something you need to look at, or you want to view it while switching code files. You can also use the pin icon to float it, or snap it back to the line of code. If you close the Exception Helper, simply click on the exception error indicator to bring it back up.

If you need to turn off the Exception Helper go to Debug/Options, scroll down the list and un-check the box for “Use the Exception Helper”.

}

4:53:50 Break on Exceptions Thrown only from Specific Modules in Visual Studio 2017 {http://bit.ly/2vFTulH

如何在指定模組中才進行偵錯

So in Visual Studio 2017 we are introducing a new feature that allows you to control breaking on thrown exceptions beyond just type. You can add a Module Name condition to an exception so you will only break on exceptions thrown from modules you care about.

5:02:30 By introducing a Module Name condition on your thrown exceptions, you can now limit the number of unnecessary breaks you encounter while debugging.

In Visual Studio 2017 we support only one type of condition, the “Module Name” condition. For any module I can decide if I only want to break on exceptions thrown from that module (Module Name Equals) or if I want to ignore breaking on exceptions thrown from that module (Module Name Not Equals).

5:04:20 Adding Module Name Condition

5:08:10 Open Debug->Windows->Exception Settings

Click on an exception type or category

Right click on that exception and choose “Edit Conditions”

Or click the blue edit pencil in the toolbar

From the Edit Conditions dialog:

Set the condition using “Equals” or “Not Equals” (e.g. “Module Name Not Equals *Settings*”)

星號 asterisks :Use asterisks (*) as wildcards before or after a keyword to match with module names containing that keyword

Click OK to set the condition

}

5:17:40 什麼是「Managed 程式碼」?{http://bit.ly/2gm4u5L

簡單來說,Managed 程式碼就是其執行受到執行階段管理的程式碼。 在此情況下,不論實作為何 (Mono、.NET Framework 或 .NET Core),所提到的執行階段都稱為 Common Language Runtime 或 CLR。 CLR 負責將 Managed 程式碼編譯成機器碼,再加以執行。 此外,執行階段提供幾項重要服務,例如自動記憶體管理、安全性界限、型別安全等。

與此相反的是您執行 C/C++ 程式的方式,又稱為「Unmanaged 程式碼」。 在 Unmanaged 世界中,程式設計人員會負責處理幾乎所有工作。 實際程式基本上是作業系統 (OS) 載入記憶體並啟動的二進位檔。 從記憶體管理到安全性考量等其他工作都是程式設計人員的責任。

Managed 程式碼是以其中一種高階語言撰寫而成 (例如 C#、Visual Basic、F# 等),並可在 .NET 平台上執行。 當您使用這些語言各自的編譯器來編譯以這些語言撰寫的程式碼時,您不會取得機器碼。 您會取得稍後可供執行階段編譯及執行的中繼語言碼。 這項規則的唯一例外是 C++,因為它也會產生在 Windows 上執行的原生、Unmanaged 二進位檔。

5:21:10 什麼是「中繼語言」(簡稱 IL)? 它是以高階 .NET 語言撰寫之程式碼的編譯結果。

當您編譯以其中一種語言撰寫的程式碼之後,您會取得由 IL 所組成的二進位檔。 請務必注意,IL 與在執行階段上執行的任何特定語言都無關;它甚至有個別的規格,如果您想要,可以閱讀此規格。

當您從高階程式碼產生 IL 之後,您可能想要執行它。 此時 CLR 會接管,並開始 Just-In-Time 編譯 (或 JIT-ing) 的程序,以將您的程式碼從 IL 編譯成可實際在 CPU 上執行的機器碼。 如此一來,CLR 就會知道您程式碼的實際功能,並可有效地進行「管理」。

中繼語言有時也稱為通用中間語言 (CIL) 或 Microsoft Intermediate Language (MSIL)。

當然,CLR 允許超過 Managed 與 Unmanaged 世界之間的界限,而且有許多程式碼會這樣做,甚至是在基底類別庫中。 這稱為互通性 或簡稱 Interop。 這些佈建可讓您包裝並呼叫 Unmanaged 程式庫。 不過,請務必請注意,一旦您這樣做,當程式碼超過執行階段的界限時,執行的實際管理會再次交由 Unmanaged 程式碼,因此會受到相同的限制。

同樣地,C# 語言可讓您在程式碼中直接使用 Unmanaged 建構 (例如資料指標),方法是利用所謂的 unsafe 內容,來指定程式碼片段的執行不受 CLR 管理。

更多資源

.NET Framework 的概觀

Unsafe 程式碼和指標

互通性 (C# 程式設計手冊)

}

受控代碼 代管代碼 託管代碼 managed code  {http://bit.ly/2iDpvd4

機器碼 :机器语言(machine language)是一種指令集的體系。这种指令集称为機器碼(machine code),是電腦的CPU可直接解讀的資料。 機器碼有時也被稱為原生碼(Native Code),這個名詞比較強調某種程式語言或函式庫與執行平台相關的部份。

}

5:34:00 What Is Managed Code?{http://bit.ly/2vnkSKc

By using managed code and compiling in this managed execution environment, you can avoid many typical programming mistakes that lead to security holes and unstable applications. Also, many unproductive programming tasks are automatically taken care of, such as type safety checking, memory management, and destruction of unneeded objects. You can therefore focus on the business logic of your applications and write them using fewer lines of code. The result is shorter development time and more secure and stable applications.

}

}


留言

熱門文章