嗨,有一个跟进另一个问题here
这是摘要
在c#中的wpf应用程序中,我有一个更新远程数据库的漫长过程 . 为此我创建了一个后台工作者 . 但是我想在数据库更新例程期间打开一个窗口并运行一个进度条 . 由于我的进度条上的“嗖嗖”效果在我的主窗口上没有开始运行直到我的后台工作线程完成后,我在主窗口上实现此操作的所有尝试都失败了,因为进度条设置为Indeterminate失败
任何有帮助和本文here我设法在不同的线程中打开一个新窗口并运行后台工作程序并正确地使用进度条"swish"并且后台工作运行完成 . 但是,我的新问题是
How do I close my progressbar window (called progressDialog) once my background work is finished?
请记住,我对此很陌生,代码示例会非常受欢迎,我想,但我不确定我是否要关闭后台工作程序RunWorkerCompleted代码区域的进度条窗口
这是我的代码
我将后台工作者设置为
public partial class MainWindow : Window
{
//Declare background workers
BackgroundWorker bwLoadCSV = new BackgroundWorker();
//Declare class variables
// some stuff
public MainWindow()
{
InitializeComponent();
//assign events to backgroundworkers
bwLoadCSV.WorkerReportsProgress = true;
bwLoadCSV.WorkerSupportsCancellation = true;
bwLoadCSV.DoWork += new DoWorkEventHandler(bwLoadCSV_DoWork);
bwLoadCSV.ProgressChanged += new ProgressChangedEventHandler(bwLoadCSV_ProgressChanged);
bwLoadCSV.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwLoadCSV_RunWorkerCompleted);
}
}
我在按钮点击事件上运行事件;
private void CSV_Load_Click(object sender, RoutedEventArgs e)
///Function to read csv into datagrid
///
{
//Turn Cursor to wait
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
Thread backgroundThread = new Thread(
new ThreadStart(() =>
{
ProgressDialog progressDialog = new ProgressDialog();
progressDialog.tbEvent.Text = "Loading CSV Data....";
progressDialog.progressBar1.IsIndeterminate = true;
progressDialog.ShowDialog();
}
));
backgroundThread.SetApartmentState(ApartmentState.STA);
backgroundThread.Start();
//Test connection to sql server
if (CHHoursDataProvider.IsDatabaseOnline() == false)
{
System.Windows.Forms.MessageBox.Show("Can not establish contact with sql server" + "\n" + "Contact IT", "Connection Error");
//Set UI picture
return;
}
//Set a control to update the user here
tbLoadDgStat.Visibility = Visibility.Visible;
//tbLoadDgStat.Text = "Getting data templete from Database...";
string FilePath = txFilePath.Text;
if (bwLoadCSV.IsBusy != true)
{
//load the context object with parameters for Background worker
bwCSVLoadContext Context = new bwCSVLoadContext();
Context.Site = cBChSite.Text;
Context.FilePath = txFilePath.Text;
Context.FileName = fileTest;
Context.Wageyear = cbWageYear.Text;
Context.Startdate = ((DateTime)dpStartDate.SelectedDate);
Context.Enddate = ((DateTime)dpEndDate.SelectedDate);
bwLoadCSV.RunWorkerAsync(Context);
}
}
我的进度条表单progressDialog xaml和class就是这个;
<Window x:Class="Test_Read_CSV.ProgressDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Progress Dialog" Height="115" Width="306" Name="ProgressPopup">
<Grid>
<ProgressBar Height="31" HorizontalAlignment="Left" Margin="12,33,0,0" Name="progressBar1" VerticalAlignment="Top" Width="250" x:FieldModifier="public" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="7,4,0,0" Name="tbEvent" VerticalAlignment="Top" Width="254" IsReadOnly="True" IsEnabled="False" x:FieldModifier="public" />
</Grid>
class is
public partial class ProgressDialog : Window
{
public ProgressDialog()
{
WindowStartupLocation = WindowStartupLocation.CenterScreen;
InitializeComponent();
progressBar1.IsIndeterminate = true;
}
我的后台工作人员完成了代码
private void bwLoadCSV_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
bwCSVLoadContext Context = e.Result as bwCSVLoadContext;
Thread.Sleep(5000);
if ((e.Cancelled == true))
{
this.tbLoadDgStat.Text = "Canceled!";
System.Threading.Thread.Sleep(1000);
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
tbLoadDgStat.Visibility = Visibility.Hidden;
}
else if (!(e.Error == null))
{
//this.tbProgress.Text = ("Error: " + e.Error.Message);
}
else
{
if (Context.LoadResult == true)
{
this.dgCSVData.DataContext = oTable.DefaultView;
btUpload.IsEnabled = true;
}
**//close the progressbar window some how here!!!**
//On the main window
this.tbLoadDgStat.Text = "Complete";
progressBar1.Value = 100;
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
tbLoadDgStat.Visibility = Visibility.Hidden;
}
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
}
1 回答
在进度页面中执行背景
如果您需要传递(并返回)一个对象,请在Progress ctor中执行此操作