Refactore MessagePumpManager to improve performance (no more reflection on startup).

This commit is contained in:
Lorenz Cuno Klopfenstein 2010-10-05 22:38:45 +02:00
parent dcc218366f
commit 7128038eb0
3 changed files with 15 additions and 54 deletions

View file

@ -4,6 +4,7 @@ using System.Text;
using System.Reflection;
using System.Windows.Forms;
using OnTopReplica.Native;
using OnTopReplica.MessagePumpProcessors;
namespace OnTopReplica {
class MessagePumpManager : IDisposable {
@ -12,6 +13,15 @@ namespace OnTopReplica {
public MainForm Form { get; private set; }
private void Register(IMessagePumpProcessor processor, MainForm form) {
_processors[processor.GetType()] = processor;
processor.Initialize(form);
#if DEBUG
Console.WriteLine("Registered message pump processor: {0}", processor.GetType());
#endif
}
/// <summary>
/// Instantiates all message pump processors and registers them on the main form.
/// </summary>
@ -29,18 +39,10 @@ namespace OnTopReplica {
#endif
}
foreach (var t in Assembly.GetExecutingAssembly().GetTypes()) {
if (typeof(IMessagePumpProcessor).IsAssignableFrom(t) && !t.IsAbstract) {
var instance = (IMessagePumpProcessor)Activator.CreateInstance(t);
instance.Initialize(form);
_processors[t] = instance;
#if DEBUG
Console.WriteLine("Registered message pump processor: {0}", t);
#endif
}
}
//Register message pump processors
Register(new WindowKeeper(), form);
Register(new HotKeyManager(), form);
Register(new GroupSwitchManager(), form);
}
/// <summary>

View file

@ -1,41 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using OnTopReplica.Native;
using System.Runtime.InteropServices;
namespace OnTopReplica.MessagePumpProcessors {
class TitleSetter : BaseMessagePumpProcessor {
const string Title = "OnTopReplica";
public override bool Process(ref Message msg) {
switch (msg.Msg) {
case WM.GETTEXT: {
Console.WriteLine("GetText");
int maxLen = msg.WParam.ToInt32();
byte[] strBytes = Encoding.UTF8.GetBytes(Title);
byte[] termBytes = new byte[strBytes.Length + 1];
strBytes.CopyTo(termBytes, 0);
termBytes[strBytes.Length] = 0;
Marshal.Copy(termBytes, 0, msg.LParam, Math.Min(maxLen, Title.Length + 1));
}
goto case WM.GETTEXTLENGTH;
case WM.GETTEXTLENGTH:
Console.WriteLine("GetTextLength");
msg.Result = (IntPtr)Title.Length;
return true;
}
return false;
}
protected override void Shutdown() {
}
}
}

View file

@ -124,7 +124,6 @@
<Compile Include="MessagePumpProcessors\GroupSwitchManager.cs" />
<Compile Include="IMessagePumpProcessor.cs" />
<Compile Include="MessagePumpProcessors\BaseMessagePumpProcessor.cs" />
<None Include="MessagePumpProcessors\TitleSetter.cs" />
<Compile Include="MessagePumpProcessors\WindowKeeper.cs" />
<Compile Include="Native\ErrorMethods.cs" />
<Compile Include="Native\HookMethods.cs" />
@ -149,6 +148,7 @@
<Compile Include="SidePanels\GroupSwitchPanel.Designer.cs">
<DependentUpon>GroupSwitchPanel.cs</DependentUpon>
</Compile>
<Compile Include="StartupOptions\Options.cs" />
<Compile Include="StoredRegionComparer.cs" />
<Compile Include="Native\WindowsSevenMethods.cs" />
<None Include="Native\CommonControls.cs" />