From 8ed71a4bf3bf290f493a1d387399878f66b1c416 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Srbeck=C3=BD?= Date: Wed, 22 Jul 2009 17:31:55 +0000 Subject: [PATCH] Created ThreadCollection class git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@4513 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Src/Pads/RunningThreadsPad.cs | 8 +-- .../Project/Src/Control/Process-Threads.cs | 60 ++++++------------- .../Project/Src/Control/Process.cs | 1 + .../Project/Src/Control/Thread.cs | 1 + .../Project/Src/Internal/ManagedCallback.cs | 12 ++-- .../Project/Src/TestPrograms/ThreadName.cs | 4 +- 6 files changed, 33 insertions(+), 53 deletions(-) diff --git a/src/AddIns/Misc/Debugger/Debugger.AddIn/Project/Src/Pads/RunningThreadsPad.cs b/src/AddIns/Misc/Debugger/Debugger.AddIn/Project/Src/Pads/RunningThreadsPad.cs index aa63323ca5..499f8ddf71 100644 --- a/src/AddIns/Misc/Debugger/Debugger.AddIn/Project/Src/Pads/RunningThreadsPad.cs +++ b/src/AddIns/Misc/Debugger/Debugger.AddIn/Project/Src/Pads/RunningThreadsPad.cs @@ -102,12 +102,12 @@ namespace ICSharpCode.SharpDevelop.Gui.Pads { if (debuggedProcess != null) { debuggedProcess.Paused -= debuggedProcess_Paused; - debuggedProcess.ThreadStarted -= debuggedProcess_ThreadStarted; + debuggedProcess.Threads.Added -= debuggedProcess_ThreadStarted; } debuggedProcess = process; if (debuggedProcess != null) { debuggedProcess.Paused += debuggedProcess_Paused; - debuggedProcess.ThreadStarted += debuggedProcess_ThreadStarted; + debuggedProcess.Threads.Added += debuggedProcess_ThreadStarted; } runningThreadsList.Items.Clear(); RefreshPad(); @@ -118,9 +118,9 @@ namespace ICSharpCode.SharpDevelop.Gui.Pads RefreshPad(); } - void debuggedProcess_ThreadStarted(object sender, ThreadEventArgs e) + void debuggedProcess_ThreadStarted(object sender, CollectionItemEventArgs e) { - AddThread(e.Thread); + AddThread(e.Item); } public override void RefreshPad() diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process-Threads.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process-Threads.cs index 88ae0a5303..07658b1ea3 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process-Threads.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process-Threads.cs @@ -14,67 +14,45 @@ namespace Debugger { public partial class Process { - Thread selectedThread; - List threadCollection = new List(); + ThreadCollection threads; - public event EventHandler ThreadStarted; - - protected virtual void OnThreadStarted(Thread thread) - { - if (ThreadStarted != null) { - ThreadStarted(this, new ThreadEventArgs(thread)); - } + public ThreadCollection Threads { + get { return threads; } } public Thread SelectedThread { - get { - return selectedThread; - } - set { - selectedThread = value; - } + get { return this.Threads.Selected; } + set { this.Threads.Selected = value; } } + } + + public class ThreadCollection: CollectionWithEvents + { + public ThreadCollection(NDebugger debugger): base(debugger) {} - public IList Threads { - get { - List threads = new List(); - foreach(Thread thread in threadCollection) { - if (!thread.HasExited) { - threads.Add(thread); - } - } - return threads.AsReadOnly(); - } + Thread selected; + + public Thread Selected { + get { return selected; } + set { selected = value; } } - internal bool ContainsThread(ICorDebugThread corThread) + internal bool Contains(ICorDebugThread corThread) { - foreach(Thread thread in threadCollection) { + foreach(Thread thread in this) { if (thread.CorThread == corThread) return true; } return false; } - internal Thread GetThread(ICorDebugThread corThread) + internal Thread Get(ICorDebugThread corThread) { - foreach(Thread thread in threadCollection) { + foreach(Thread thread in this) { if (thread.CorThread == corThread) { return thread; } } - throw new DebuggerException("Thread is not in collection"); } - - internal void AddThread(ICorDebugThread corThread) - { - Thread thread = new Thread(this, corThread); - threadCollection.Add(thread); - OnThreadStarted(thread); - - thread.Exited += delegate { - threadCollection.Remove(thread); - }; - } } } diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process.cs index 84415126cf..3c1ed3b008 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process.cs @@ -74,6 +74,7 @@ namespace Debugger evals = new EvalCollection(debugger); modules = new ModuleCollection(debugger); + threads = new ThreadCollection(debugger); } internal ICorDebugProcess CorProcess { diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Thread.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Thread.cs index 06fd59e67c..624aef587d 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Thread.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Thread.cs @@ -100,6 +100,7 @@ namespace Debugger this.HasExited = true; OnExited(new ThreadEventArgs(this)); + process.Threads.Remove(this); } protected virtual void OnExited(ThreadEventArgs e) diff --git a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Internal/ManagedCallback.cs b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Internal/ManagedCallback.cs index 2b5cff7307..acf4240695 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Internal/ManagedCallback.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Internal/ManagedCallback.cs @@ -78,7 +78,7 @@ namespace Debugger void EnterCallback(PausedReason pausedReason, string name, ICorDebugThread pThread) { EnterCallback(pausedReason, name, pThread.Process); - process.SelectedThread = process.GetThread(pThread); + process.SelectedThread = process.Threads.Get(pThread); } void ExitCallback() @@ -123,7 +123,7 @@ namespace Debugger { EnterCallback(PausedReason.StepComplete, "StepComplete (" + reason.ToString() + ")", pThread); - Thread thread = process.GetThread(pThread); + Thread thread = process.Threads.Get(pThread); Stepper stepper = thread.GetStepper(pStepper); StackFrame currentStackFrame = process.SelectedThread.MostRecentStackFrame; @@ -354,7 +354,7 @@ namespace Debugger EnterCallback(PausedReason.Other, "NameChange: pThread", pThread); - Thread thread = process.GetThread(pThread); + Thread thread = process.Threads.Get(pThread); thread.NotifyNameChanged(); ExitCallback(); @@ -368,7 +368,7 @@ namespace Debugger // and we continue from this callback anyway EnterCallback(PausedReason.Other, "CreateThread " + pThread.ID, pAppDomain); - process.AddThread(pThread); + process.Threads.Add(new Thread(process, pThread)); ExitCallback(); } @@ -410,10 +410,10 @@ namespace Debugger public void ExitThread(ICorDebugAppDomain pAppDomain, ICorDebugThread pThread) { // ICorDebugThread is still not dead and can be used for some operations - if (process.ContainsThread(pThread)) { + if (process.Threads.Contains(pThread)) { EnterCallback(PausedReason.Other, "ExitThread " + pThread.ID, pThread); - process.GetThread(pThread).NotifyExited(); + process.Threads.Get(pThread).NotifyExited(); } else { EnterCallback(PausedReason.Other, "ExitThread " + pThread.ID, process.CorProcess); diff --git a/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/ThreadName.cs b/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/ThreadName.cs index ec75fd8b78..717bf4e0c4 100644 --- a/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/ThreadName.cs +++ b/src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/ThreadName.cs @@ -39,8 +39,8 @@ namespace Debugger.Tests { void debugger_ProcessStarted(object sender, CollectionItemEventArgs e) { - e.Item.ThreadStarted += delegate(object sender2, ThreadEventArgs f) { - ObjectDump("ThreadStartedEvent", f.Thread); + e.Item.Threads.Added += delegate(object sender2, CollectionItemEventArgs f) { + ObjectDump("ThreadStartedEvent", f.Item); }; } }