我使用了两个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。
感谢您的帮助。
答案 0 :(得分:0)
您的代码的一般问题是您使用Task
来执行不执行任何异步I / O工作的代码。 CreateHighlighter
和DoStuffAsync
都被称为Task
,绝对没有充分的理由
此外,由于CreateHighlighter
正在使用UI对象,因为您不在UI上下文中(由于您使用async
),因此更难以制作Task.Run
。这就是你的代码的样子:
public sealed partial class MainPage : Page
{
...
private async void btnFind_Click(object sender, RoutedEventArgs e)
{
...
List<int> indexList = await Task.Run(DoStuff(completeText, tofind));
TextHighlighter HighlighterAll = CreateHighlighter(indexList, tofind.Length);
...
}
private TextHighlighter CreateHighlighter(List<int> listaindex, int lenght)
{
...
return Higlighter;
}
private List<int> DoStuff(string myTxt, string toFind)
{
...
return indexList;
}
}