我是C#,WPF和线程的新手.我正在使用MVC5开发Web应用程序.
我知道我应该在更新主线程以外的线程中的UI元素时调度调度程序.但我并不完全了解我需要做的改变.
我有一个GetBitmapForDistributionChart方法,它可以像这样进行UI更新.
public byte[] GetBitmapForDistributionChart(int width, int height, DistributionChartParameters disrtibutionParams)
{
// delegate control instantiation to STA Thread
return DelegateBitmapGenerationToSTAThread(() =>
{
Chart canvasChart = new Chart(languageService);
canvasChart.Width = width;
canvasChart.Height = height;
canvasChart.Measure(new Size(width, height));
canvasChart.Arrange(new Rect(0, 0, width, height));
return GenerateBitmapFromControl(canvasChart, width, height);
});
}
Run Code Online (Sandbox Code Playgroud)
DelegateBitmapGenerationToSTAThread的定义如下所示:
private byte[] DelegateBitmapGenerationToSTAThread(Func<byte[]> controlBitmapGenerator)
{
byte[] imageBinaryData = null;
Thread thread = new Thread(() =>
{
var renderer = new BitmapCreator(this.languageService);
imageBinaryData = controlBitmapGenerator();
});
//Set the thread to STA
thread.SetApartmentState(ApartmentState.STA); …Run Code Online (Sandbox Code Playgroud) 我在Windows 7上使用Git的Source tree客户端。我已经使用了tortoisehg客户端进行比较,并且在SourceTree中也使用Beyond比较自身作为diff。我在工具->选项中将diff工具设置为无法比较,但不确定如何通过任何文件的源树启动diff。双击文件通常会显示差异视图。右键单击->自定义操作也不会执行任何操作。
比较3.3.13和源树2.1.2.5
请让我知道如何配置。
我在xaml中有一个按钮,其鼠标点击连接到委托命令.在委托命令中,我调用另一个创建位图并将其保存为pdf的方法.此方法需要几秒钟(通常超过10秒),在此期间应用程序进入无响应状态,无法访问UI.在此期间,我想展示一些已经在xaml中定义的动画.但是在调用time-taking-function之前显示它并在之后隐藏它并不起作用.如果我不使用不同的线程,它会在时间执行功能执行结束时显示.
所以,我尝试使用创建一个不同的线程并调用此时间函数.动画加载正常但该函数实际上需要访问UI线程中的一些UI对象.它导致了这个异常:"调用线程无法访问此对象,因为不同的线程拥有它."
有没有办法实现它?即,在此功能执行期间显示动画?
一些代码片段:
private void OnExportPDFCommand()
{
PanelLoading = true; // Flag to show animation
Task.Factory.StartNew(() =>
{
CreatePDF(); //time-taking-function
Application.Current.Dispatcher.Invoke(() => PanelLoading = false);
});
}
Run Code Online (Sandbox Code Playgroud)
请用一个小例子来解释.我是WPF的新手.谢谢.