【发布时间】:2018-07-10 11:55:02
【问题描述】:
我使用了两个Task.Run。在第一个中一切正常,但在第二个中引发异常:
来自 HRESULT 的异常:0x8001010E (RPC_E_WRONG_THREAD)。
我不明白如何解决它。我想从代码中了解Task.Run 返回TextHighlighter。什么是正确的解决方案?
xaml:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid x:Name="CommandGrid" Margin="25,50,0,50" Width="200" HorizontalAlignment="Left">
<StackPanel x:Name="CommandStack" Orientation="Vertical">
<Button x:Name="btnFind" Click="btnFind_Click" Content="Find Words" HorizontalAlignment="Stretch" Height="32"/>
<TextBox x:Name="txbToFind" HorizontalAlignment="Stretch" Height="32" Margin="0,20,0,0"/>
</StackPanel>
</Grid>
<Grid x:Name="BaseGrid" Margin="250,50,50,50" Background="#FFCBF3A6">
<ScrollViewer x:Name="BaseScroll">
<RichTextBlock x:Name="TextOneRich" Margin="20,20,35,20"/>
</ScrollViewer>
</Grid>
</Grid>
xaml.cs:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new Run() { Text = "Paste the text here" });
TextOneRich.Blocks.Add(paragraph);
}
private async void btnFind_Click(object sender, RoutedEventArgs e)
{
string tofind = txbToFind.Text.ToLower();
string completeText = string.Empty;
for (int a = 0; a <= TextOneRich.Blocks.Count - 1; a++)
{
Paragraph paragraphCurrent = TextOneRich.Blocks[a] as Paragraph;
for (int b = 0; b <= paragraphCurrent.Inlines.Count - 1; b++)
{
completeText += (paragraphCurrent.Inlines[b] as Run).Text;
}
}
List<int> indexList = await Task.Run(async () => await DoStuffAsync(completeText, tofind)); // Works well
TextHighlighter HighlighterAll = await Task.Run(async () => await CreateHighlighter(indexList, tofind.Length)); // Generate exception
TextOneRich.TextHighlighters.Clear();
TextOneRich.TextHighlighters.Add(HighlighterAll);
}
private async Task<TextHighlighter> CreateHighlighter(List<int> listaindex, int lenght)
{
TextHighlighter Higlighter = new TextHighlighter() { Foreground = new SolidColorBrush(Colors.White), Background = new SolidColorBrush(Color.FromArgb(255, 7, 58, 77)) };
for (int a = 0; a <= listaindex.Count - 1; a++)
{
Higlighter.Ranges.Add(new TextRange() { StartIndex = listaindex[a], Length = lenght });
}
return await Task.FromResult(Higlighter);
}
private async Task<List<int>> DoStuffAsync(string myTxt, string toFind)
{
bool thereis = true;
List<int> indexList = new List<int>();
string remainingText = string.Empty;
remainingText = myTxt;
int progressiveIndex = 0;
int index = 0;
while (thereis)
{
if (remainingText.ToLower().IndexOf(toFind) != -1)
{
indexList.Add(remainingText.ToLower().IndexOf(toFind) + progressiveIndex);
index = remainingText.ToLower().IndexOf(toFind) + 1;
progressiveIndex += index;
remainingText = remainingText.Substring(index, remainingText.Length - index);
}
else
{
thereis = false;
}
}
return await Task.FromResult(indexList);
}
}
要粘贴的文本:text。
异常截图:exception.
感谢您的帮助。
【问题讨论】:
-
您的
DoStuffAsync方法似乎没有执行任何必须在 UI 线程上执行的操作。CreateHighligher会。 -
为什么会产生异常?
-
我更正了代码。
-
代码有很多问题。 :( 异常应该相当清楚:您在错误的线程中执行代码。有关此信息,请参阅标记的重复项。您可以按照该建议解决问题,但您有更基本的问题:工作将在 UI 线程上结束,在您执行此操作时将其阻塞。解决此问题的唯一方法是分批完成工作,并允许 UI 线程有时间定期刷新。不幸的是,没有@987654324 @这里,没有很好的方法来解决你真正遇到的问题。查看标记的重复以解决你提出的问题