我正在执行一些操作,比如读取csv文件并转换为对象。除了busyindicator.Isbusy =true时BusyIndicator不可见之外,一切都运行正常;
所有的东西都在主线程上运行,所以我猜当我读取文件时,UI或主线程可能会很忙,因为它看不见。
代码:
private void ImportData(Dictionary<string, ImportFieldMap> mappedItems)
{
var fileBytes = fileBrowseCtrl.FileBytes;
var getDelimiter = string.IsNullOrEmpty(txeSeperator.Text) ? UtilFunctions.GetDefaultDeLimiter() : txeSeperator.Text.ToCharArray()[0];
if (fileBytes == null)
{
MessageBox.Show(Uniconta.ClientTools.Localization.lookup("NoFilesSelected"), Uniconta.ClientTools.Localization.lookup("Error"), MessageBoxButton.OK);
return;
}
Encoding Coding = Encoding.Unicode;
if (ANSICodePage.IsUTF8(fileBytes))
Coding = Encoding.UTF8;
else if (ANSICodePage.IsANSI(fileBytes))
{
fileBytes = ANSICodePage.Convert2Unicode(fileBytes);
Coding = Encoding.Unicode;
}
try
{
busyIndicator.IsBusy = true;
CSVHelper csvData;
using (var reader = new StreamReader(new MemoryStream(fileBytes), Coding, true))
{
csvData = CSVHelper.Load(reader, getDelimiter);
}
//Converting to UnicontaObject Code...
}
catch
{
MessageBox.Show(Uniconta.ClientTools.Localization.lookup("InvalidFileFormat"), Uniconta.ClientTools.Localization.lookup("Error"), MessageBoxButton.OK);
fileBrowseCtrl.ResetControl();
}
finally
{
busyIndicator.IsBusy = false;
}
}有没有一种方法可以在不同的线程上独立运行,这样当函数被调用时,UI会显示busyIndicator,并在后台执行读取csv和转换为object的操作
我已经尝试过使用BackgroundWorkerThread,但它是异步的,所以有没有其他方法可以实现这一点?
问候
发布于 2015-11-04 15:42:11
1.多线程
1.1 BackgroungWorker就可以了。
1.2。另一种选择是使用Dispatcher
每个WPF组件都有一个Dispatcher,可以用来返回GUI线程:
// Get the dispatcher on the Gui thread
Dispatcher dispatcher = window.Dispatcher;
// use it on the worker thread to get back to GUI thread
dispatcher.Invoke(
() =>{
busyIndicator.IsBusy = true;
);图形用户界面2.更新
在GUI中没有更改的原因可能是您的GUI属性没有引发任何事件。
public bool IsBusy{
get{ return isBusy; }
set{
isBusy = value;
FirePropertyChange();
}
}并且应该在承载IsBusy属性的类上实现INotifyPropertyChanged:
public class User : IDataErrorInfo, INotifyPropertyChanged
{
private void OnNotifyPropertyChange([CallerMemberName]string propName = null)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
public event PropertyChangedEventHandler PropertyChanged;问候
https://stackoverflow.com/questions/33514664
复制相似问题