Don't use SendMessage when the built-in Invoke function does the same

This commit is contained in:
Magnus Norddahl 2019-12-27 04:37:10 +01:00
parent f7f408b34c
commit 7e0ea54254
5 changed files with 90 additions and 68 deletions

View file

@ -789,7 +789,7 @@ namespace CodeImp.DoomBuilder.Data
// Done
notifiedbusy = false;
backgroundloader = null;
General.SendMessage(General.MainWindow.Handle, (int)MainForm.ThreadMessages.UpdateStatus, IntPtr.Zero, IntPtr.Zero);
General.MainWindow.UpdateStatus();
}
}
@ -834,7 +834,7 @@ namespace CodeImp.DoomBuilder.Data
if(!notifiedbusy)
{
notifiedbusy = true;
General.SendMessage(General.MainWindow.Handle, (int)MainForm.ThreadMessages.UpdateStatus, IntPtr.Zero, IntPtr.Zero);
General.MainWindow.UpdateStatus();
}
Thread.Sleep(0);
}
@ -848,7 +848,7 @@ namespace CodeImp.DoomBuilder.Data
if(!notifiedbusy)
{
notifiedbusy = true;
General.SendMessage(General.MainWindow.Handle, (int)MainForm.ThreadMessages.UpdateStatus, IntPtr.Zero, IntPtr.Zero);
General.MainWindow.UpdateStatus();
}
Thread.Sleep(0);
}
@ -872,14 +872,13 @@ namespace CodeImp.DoomBuilder.Data
if(notifiedbusy)
{
notifiedbusy = false;
IntPtr strptr = Marshal.StringToCoTaskMemAuto(deltatimesec);
General.SendMessage(General.MainWindow.Handle, (int)MainForm.ThreadMessages.ResourcesLoaded, strptr, IntPtr.Zero);
General.MainWindow.ResourcesLoaded(deltatimesec);
}
}
else if(notifiedbusy) //mxd. Sould never happen (?)
{
notifiedbusy = false;
General.SendMessage(General.MainWindow.Handle, (int)MainForm.ThreadMessages.UpdateStatus, IntPtr.Zero, IntPtr.Zero);
General.MainWindow.UpdateStatus();
}
// Wait longer to release CPU resources
@ -912,7 +911,7 @@ namespace CodeImp.DoomBuilder.Data
}
// Update icon
General.SendMessage(General.MainWindow.Handle, (int)MainForm.ThreadMessages.UpdateStatus, IntPtr.Zero, IntPtr.Zero);
General.MainWindow.UpdateStatus();
}
//mxd. This loads a model

View file

@ -253,8 +253,7 @@ namespace CodeImp.DoomBuilder.Data
LocalLoadImage();
// Notify the main thread about the change so that sectors can update their buffers
IntPtr strptr = Marshal.StringToCoTaskMemAuto(this.name);
General.SendMessage(General.MainWindow.Handle, (int)MainForm.ThreadMessages.ImageDataLoaded, strptr, IntPtr.Zero);
General.MainWindow.ImageDataLoaded(this.name);
}
// This requests loading the image

View file

@ -71,8 +71,7 @@ namespace CodeImp.DoomBuilder.Data
LocalLoadImage();
// Notify the main thread about the change to redraw display
IntPtr strptr = Marshal.StringToCoTaskMemAuto(this.Name);
General.SendMessage(General.MainWindow.Handle, (int)MainForm.ThreadMessages.SpriteDataLoaded, strptr, IntPtr.Zero);
General.MainWindow.SpriteDataLoaded(this.Name);
}
// This loads the image

View file

@ -59,8 +59,7 @@ namespace CodeImp.DoomBuilder.Data
LocalLoadImage();
// Notify the main thread about the change to redraw display
IntPtr strptr = Marshal.StringToCoTaskMemAuto(this.Name);
General.SendMessage(General.MainWindow.Handle, (int)MainForm.ThreadMessages.SpriteDataLoaded, strptr, IntPtr.Zero);
General.MainWindow.SpriteDataLoaded(this.Name);
}
// This loads the image

View file

@ -77,23 +77,6 @@ namespace CodeImp.DoomBuilder.Windows
}
};
// Message pump
public enum ThreadMessages
{
// Sent by the background threat to update the status
UpdateStatus = General.WM_USER + 1,
// This is sent by the background thread when images are loaded
// but only when first loaded or when dimensions were changed
ImageDataLoaded = General.WM_USER + 2,
//mxd. This is sent by the background thread when sprites are loaded
SpriteDataLoaded = General.WM_USER + 3,
//mxd. This is sent by the background thread when all resources are loaded
ResourcesLoaded = General.WM_USER + 4,
}
#endregion
#region ================== Delegates
@ -4125,49 +4108,92 @@ namespace CodeImp.DoomBuilder.Windows
}
}
#endregion
#endregion
#region ================== Message Pump
// This handles messages
protected override void WndProc(ref Message m)
#region ================== Threadsafe updates
// This is to avoid spamming the UI thread with messages
object syncobject = new object();
List<System.Action> uithreadActions = new List<System.Action>();
void ProcessUIThreadActions()
{
List<System.Action> actions;
lock (syncobject)
{
actions = uithreadActions;
uithreadActions = new List<System.Action>();
}
foreach (System.Action action in actions)
action();
}
public void RunOnUIThread(System.Action action)
{
bool notifyUIThread;
lock (syncobject)
{
notifyUIThread = uithreadActions.Count == 0;
uithreadActions.Add(action);
}
if (notifyUIThread)
Invoke(new System.Action(ProcessUIThreadActions));
}
public void UpdateStatus()
{
RunOnUIThread(() =>
{
DisplayStatus(status);
});
}
public void ImageDataLoaded(string imagename)
{
RunOnUIThread(() =>
{
if ((General.Map != null) && (General.Map.Data != null))
{
ImageData img = General.Map.Data.GetFlatImage(imagename);
ImageDataLoaded(img);
}
});
}
public void SpriteDataLoaded(string spritename)
{
RunOnUIThread(() =>
{
if ((General.Map != null) && (General.Map.Data != null))
{
ImageData img = General.Map.Data.GetSpriteImage(spritename);
if (img != null && img.UsedInMap && !img.IsDisposed)
{
DelayedRedraw();
}
}
});
}
public void ResourcesLoaded(string loadtime)
{
RunOnUIThread(() =>
{
DisplayStatus(StatusType.Info, "Resources loaded in " + loadtime + " seconds");
});
}
#endregion
#region ================== Message Pump
// This handles messages
protected override void WndProc(ref Message m)
{
// Notify message?
switch(m.Msg)
{
case (int)ThreadMessages.UpdateStatus:
DisplayStatus(status);
break;
case (int)ThreadMessages.ImageDataLoaded:
string imagename = Marshal.PtrToStringAuto(m.WParam);
Marshal.FreeCoTaskMem(m.WParam);
if((General.Map != null) && (General.Map.Data != null))
{
ImageData img = General.Map.Data.GetFlatImage(imagename);
ImageDataLoaded(img);
}
break;
case (int)ThreadMessages.SpriteDataLoaded: //mxd
string spritename = Marshal.PtrToStringAuto(m.WParam);
Marshal.FreeCoTaskMem(m.WParam);
if((General.Map != null) && (General.Map.Data != null))
{
ImageData img = General.Map.Data.GetSpriteImage(spritename);
if(img != null && img.UsedInMap && !img.IsDisposed)
{
DelayedRedraw();
}
}
break;
case (int)ThreadMessages.ResourcesLoaded: //mxd
string loadtime = Marshal.PtrToStringAuto(m.WParam);
Marshal.FreeCoTaskMem(m.WParam);
DisplayStatus(StatusType.Info, "Resources loaded in " + loadtime + " seconds");
break;
case General.WM_SYSCOMMAND:
// We don't want to open a menu when ALT is pressed
if(m.WParam.ToInt32() != General.SC_KEYMENU)