Skip to content

Commit

Permalink
Fix LT-21934: fork task to stop parser so control returns to user
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmaxwell3 committed Nov 18, 2024
1 parent 491a772 commit e46f97d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
20 changes: 20 additions & 0 deletions Src/LexText/ParserCore/ParseFiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ public class ParseFiler

#region Properties

/// <summary>
/// Are we in the process of stopping the thread this is running in?
/// </summary>
public bool Stopping;

/// <summary>
/// Are we updating word forms?
/// </summary>
public bool UpdatingWordforms;

#endregion Properties

#region Construction and Disposal
Expand Down Expand Up @@ -108,6 +118,9 @@ public ParseFiler(LcmCache cache, Action<TaskReport> taskUpdateHandler, IdleQueu
m_baseAnnotationRepository = servLoc.GetInstance<ICmBaseAnnotationRepository>();
m_baseAnnotationFactory = servLoc.GetInstance<ICmBaseAnnotationFactory>();
m_userAgent = m_cache.LanguageProject.DefaultUserAgent;

Stopping = false;
UpdatingWordforms = false;
}

#endregion Construction and Disposal
Expand Down Expand Up @@ -145,6 +158,12 @@ private bool UpdateWordforms(object parameter)
if (!((IActionHandlerExtensions) m_cache.ActionHandlerAccessor).CanStartUow)
return false;

// Don't update words if we are stopping.
if (Stopping)
return false;
// Don't stop if we are updating words.
UpdatingWordforms = true;

// update all of the wordforms in a batch, this might slow down the UI thread a little, if it causes too much unresponsiveness
// we can bail out early if there is a message in the Win32 message queue
IEnumerable<WordformUpdateWork> results;
Expand Down Expand Up @@ -208,6 +227,7 @@ from ann in m_baseAnnotationRepository.AllInstances()
FireWordformUpdated(work.Wordform, work.Priority, work.ParseResult, work.CheckParser);
}
});
UpdatingWordforms = false;
return true;
}

Expand Down
15 changes: 15 additions & 0 deletions Src/LexText/ParserCore/ParserScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using SIL.LCModel;
using SIL.ObjectModel;
using XCore;
using System.Threading;

namespace SIL.FieldWorks.WordWorks.Parser
{
Expand Down Expand Up @@ -196,6 +197,20 @@ public bool TryAWordDialogIsRunning
}

protected override void DisposeManagedResources()
{
// Wait until wordforms aren't being updated.
m_parserWorker.ParseFiler.Stopping = true;
while (m_parserWorker.ParseFiler.UpdatingWordforms)
Thread.Sleep(1);
// It should be safe to stop the thread now.
// (Thread.Stop aborts after 60 seconds, which can leave things in a funny state.)
System.Threading.Tasks.Task.Run(() =>
{
FinishDisposeManagedResources();
});
}

private void FinishDisposeManagedResources()
{
m_thread.Stop();
m_thread.Dispose();
Expand Down
9 changes: 0 additions & 9 deletions Src/LexText/ParserUI/ParserUIStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions Src/LexText/ParserUI/ParserUIStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,4 @@
<data name="ksEnterComment" xml:space="preserve">
<value>Please enter a comment for the parser report</value>
</data>
<data name="ksStoppingParser" xml:space="preserve">
<value>Stopping the Parser (may take up to a minute)</value>
</data>
</root>
1 change: 0 additions & 1 deletion Src/LexText/ParserUI/TryAWordDlg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ protected override void OnClosed(EventArgs ea)
m_persistProvider.PersistWindowSettings(PersistProviderID, this);
if (m_parserListener.Connection != null)
{
this.Text = ParserUIStrings.ksStoppingParser;
m_parserListener.Connection.TryAWordDialogIsRunning = false;
m_parserListener.DisconnectFromParser();
}
Expand Down

0 comments on commit e46f97d

Please sign in to comment.