关闭在与后台工作线程不同的线程上创建的 wpf 窗口 工作运行已完成
本文关键字:工作 线程 创建 wpf 已完成 运行 窗口 后台 | 更新日期: 2023-09-27 18:35:12
嗨,这里有另一个问题的后续
问题这是摘要
在 c# 的 wpf 应用程序中,我有一个很长的更新远程数据库的过程。 为此,我创建了一个后台工作线程。但是,我想在数据库更新例程期间打开一个窗口并运行一个进度条。我在进度条设置为不确定的主窗口上实现此目的的所有尝试都失败了,因为进度条上的"嗖嗖"效果直到我的后台工作线程完成后才开始在我的主窗口上运行
任何帮助和这篇文章在这里,我设法在不同的线程中打开一个新窗口并运行后台工作程序并让进度条正确"嗖嗖",后台工作运行完成。但是,我的新问题是
后台工作完成后,如何关闭进度条窗口(称为进度对话框)?
请记住,我对此很陌生,代码示例将非常受欢迎,我认为,但我不确定我是否要从后台工作线程 RunWorker完成的代码区域关闭进度条窗口
这是我的代码
我将后台辅助角色设置为
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);
}
}
我的进度条表单进度对话框 xaml 和类是这个;
<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;
}
在进度页
中执行背景如果您需要传递(并返回)一个对象,请在进度 ctor 中执行此操作
private void click(object sender, RoutedEventArgs e)
{
Progress progressDialog = new Progress();
progressDialog.Show();
if (progressDialog != null) progressDialog = null;
}
namespace BackGroundWorkerShowDialog
{
/// <summary>
/// Interaction logic for Progress.xaml
/// </summary>
public partial class Progress : Window
{
BackgroundWorker bwLoadCSV = new BackgroundWorker();
public Progress()
{
InitializeComponent();
//assign events to backgroundworkers
bwLoadCSV.WorkerReportsProgress = true;
bwLoadCSV.WorkerSupportsCancellation = true;
bwLoadCSV.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
bwLoadCSV.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
bwLoadCSV.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
if (bwLoadCSV.IsBusy != true)
{
// Start the asynchronous operation.
bwLoadCSV.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; i <= 10; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500);
worker.ReportProgress(i * 10);
}
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled == true)
{
//resultLabel.Text = "Canceled!";
}
else if (e.Error != null)
{
//resultLabel.Text = "Error: " + e.Error.Message;
}
else
{
//resultLabel.Text = "Done: " + e.Error.Message;
}
this.Close();
}
private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
this.tbProgress.Text = e.ProgressPercentage.ToString();
}
}
}