保持GUI事件线程直到方法完成

时间:2014-05-18 18:08:50

标签: delphi delphi-xe2 docking jvcl pagecontrol

目前,在我的应用程序中,用户可以创建停靠在页面控件上的窗口(当前没有控件)。每个编辑器都有一组与之关联的工具箱。在编辑器构造时重置此组,并在工具箱关闭和打开时进行修改。当页面控件更改选项卡时,工具箱的状态将被保存,它们将被关闭,然后将恢复新选项卡的工具箱。

当您更改标签时,我的应用程序崩溃(最初看似随机)。我相信当你快速更改标签(双击的速度)时,PageControl.OnChange事件中工具箱的处理在下一个PageControl.OnChange事件开始之前没有完成,我最终在'并行运行两个事件”。我的问题是:

1) 这听起来像是一个看似合理的理论吗?我知道有一个GUI处理线程,delphi通过调度新线程来处理事件吗?

2) 您如何建议修复此问题?保持消息,直到第一个方法结束(这是一个非常快速的方法,所以真的不应该有任何使用延迟)?

以下是一些代码,任何人都希望从另一个角度看问题。

这是onchange事件的代码。

procedure TMainForm.PageDockingAreaChange(Sender: TObject);
var
  count : integer;

  // Shortcut variables
  FormShortcut : TForm;
  WelcomeShortcut : TWelcomePageForm;
  BaseEditorShortcut : TBaseEditor;

begin

  // Set nil values
  FormShortcut := nil;
  WelcomeShortcut := nil;
  BaseEditorShortcut := nil;

  // Create shortcut value
  FormShortcut := Self.GetActiveForm;
  if (FormShortcut is TWelcomePageForm) then WelcomeShortcut := TWelcomePageForm(FormShortcut);
  if (FormShortcut is TBaseEditor) then BaseEditorShortcut := TBaseEditor(FormShortcut);

  // Hide all tabs when welcome page is visible
  if (WelcomeShortcut <> nil) then
    begin

      // Clear any existing toolboxes
      Self.ToolboxClearAll;

    end;
  {endif}

  // Try to execute toolbox setup
  try

    // Load toolbox state
    Self.ToolboxSetup(BaseEditorShortcut);

  except

    // Display fatal error to the user
    ShowMessage('Fatal Error : Unable to load toolboxes.');

  end;

  // Update last active editor
  if (BaseEditorShortcut = nil) then
    Self.LastActiveEditor := nil
  else
    Self.LastActiveEditor := BaseEditorShortcut;
  {endif}

end;

此方法关闭所有工具箱并依次释放它们。

procedure TMainForm.ToolboxClearAll;
var
  count : integer;
begin

  // Save toolbox state if needed
  if Assigned(Self.LastActiveEditor) then
    Self.LastActiveEditor.SaveToolboxState;

  // Close and free all toolboxes
  for count := 0 to length(Self.ActiveToolboxes)-1 do
    Self.ToolboxCloseAndFree(Self.ActiveToolboxes[count]);

  // Reset array size (should already be done though)
  SetLength(Self.ActiveToolboxes, 0);

end;

0 个答案:

没有答案