我想在应用程序繁忙时在我的wpf应用程序中显示加载图标? [英] I want to show a loading icon in my wpf application when application is busy?
问题描述
我想在应用程序繁忙的时候在我的wpf应用程序中显示一个加载图标。
为了实现这个,我采取了一个后台工作者并显示我的加载BackgroundWorker_DoWork()事件上的图片。
例如:在保存按钮上单击wpf表单进程是这样的:
1.在表格上有一些ui控件,如文本框,标签和复选框。
2.在保存按钮上,我将数据发送到数据库。
3.单击保存按钮然后调用bgworker_dowork()并执行savedata()函数但是当我获得控制值时它会抛出错误:
调用线程无法访问此对象,因为另一个线程拥有它。
4.我知道这个错误即将发生,因为线程ID已经改变。但无法理清这个错误。
示例代码如下
我的背景工作者课程
Hi,
I want to show a loading icon in my wpf application when application is busy.
To implementing this I have taken a background worker and showing my loading image on BackgroundWorker_DoWork() event.
For Example: on save button click of wpf form process is something like this:
1. Have some ui controls like text box, lables and check boxes on the form.
2. On the save button I am sending the data in to database.
3. When save button is clicked then bgworker_dowork() is called and savedata() function is executed but as I getting the control value then it throws an error:
"The calling thread cannot access this object because a different thread owns it".
4. Here I understand that this error is coming because thread id has been changed. but unable to sort out this error.
Sample code as following
My backgroundworker class
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.ComponentModel;
using LoadingControl;
using System.Windows.Forms;
namespace LoadingClass
{
public class BackgroundLoading
{
public delegate void RunFunction();
public BackgroundWorker Bw;
public RunFunction thisFunction;
LoadingWindow newLoading;
public BackgroundLoading(RunFunction newFunction)
{
try
{
thisFunction = newFunction;
Bw = new BackgroundWorker();
Bw.DoWork += new DoWorkEventHandler(Bw_DoWork);
Bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Bw_RunWorkerCompleted);
}
catch (Exception ex)
{
throw;
}
}
public void Start()
{
try
{
Bw.RunWorkerAsync();
newLoading = new LoadingWindow();
newLoading.ShowDialog();
}
catch (Exception ex)
{
throw;
}
}
void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
try
{
newLoading.Close();
}
catch (Exception ex)
{
throw;
}
}
void Bw_DoWork(object sender, DoWorkEventArgs e)
{
try
{
if (thisFunction != null)
{
thisFunction();
}
}
catch (Exception ex)
{
throw;
}
}
}
}
我的wpf表单上的代码是这样的
code on my wpf form is something like this
public void btnSave_Click(object sender, RoutedEventArgs e)
{
try
{
if (Validator.IsValid(this))
{
BackgroundLoading BL = new BackgroundLoading(SavePatient);
BL.Start();
}
}
catch (Exception ex)
{
Common.WriteToErrorLog(ex.Message, "btnSave_Click");
}
}
public void SavePatient()
{
txtAge.Text = txtAge.Text; // error is coming in this line
}
如果您有任何解决方案,请提出建议......
提前致谢
Please suggest if you have any solution...
Thanks in advance
推荐答案
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Threading;
namespace TestLoading
{
/// AMIT KUMAR JHA
public partial class Window1 : Window
{
public decimal Idx;
public decimal Maximum = 90000000;
decimal nudShowResult;
DispatcherTimer dispatcherTimer = new DispatcherTimer();
BackgroundWorker bWorker1 ;
FrmLoding LoadingForm = new FrmLoding();
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
nudShowResult =Idx;
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
decimal ThisMaxValue = Maximum;
if (e.Argument != null && e.Argument is decimal)
ThisMaxValue = (decimal)e.Argument;
//This run in a new thread
for (int i = 0; i <= ThisMaxValue; i++)
{
Idx = i;
}
}
private void worker_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
nudShowResult = Idx;
dispatcherTimer.Stop();
bWorker1.RunWorkerAsync();
if (LoadingForm != null && LoadingForm.IsVisible)
//
LoadingForm.Close();
}
private void btnShow_Click(object sender, RoutedEventArgs e)
{
bWorker1 = (BackgroundWorker)this.FindResource("backWorker");
dispatcherTimer.Start();
bWorker1.RunWorkerAsync();
LoadingForm.ShowDialog();
}
public void Counter()
{
for (int i = 0; i <= Maximum; i++)
{
Idx = i;
}
}
}
}
// AMIT KUMAR JHA
< window x:class =TestBusyIndicator.Window1xmlns:x =#unknown>
xmlns =http://schemas.microsoft。 com / winfx / 2006 / xaml / presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
xmlns:wpfx =http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended
Title =Window1Height =300Width =500Loaded = Window_Loaded>
< grid>
< wpfx:busyindicator name =BusyBarmargin =134,218,135,0busycontent =Plese wait。xmlns: wpfx =#unknown>
< listbox height =148horizontalalignment =leftmargin =110, 64,0,0name =listBox1verticalalignment =Topwidth =281itemssource ={Binding}>
- - - - - - - - - - - - -码 - - - - - - - - - - - - - ---
使用System.Text;
使用System.Windows;
使用System.Windows。控件;
使用System.Windows.Data;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.Windows.Threading;
命名空间TestBusyIndicator >
{
公共部分类Window1:窗口
{
DispatcherTimer dispatcherTimer = new DispatcherTimer ();
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(对象发送者,RoutedEventArgs e)
{
dispatcherTimer.Tick + = new EventHandler(di spatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,0,2);
}
private void StartButton_Click(object sender,RoutedEventArgs e)
{
dispatcherTimer.Start();
BusyBar.IsBusy = true;
}
private void dispatcherTimer_Tick(object sender,EventArgs e)
{
listBox1.Items.Add(DateTime.Now.Hour.ToString()+:+
DateTime.Now.Second.ToString());
CommandManager.InvalidateRequerySuggested();
listBox1.Items.MoveCurrentToLast();
listBox1.SelectedItem = listBox1.Items。 CurrentItem;
listBox1.ScrollIntoView(listBox1.Items.CurrentItem);
}
private void StopButton_Click( objec t sender,RoutedEventArgs e)
{
dispatcherTimer.Stop();
BusyBar.IsBusy = false;
}
public void SavePatient()
{
}
}
}
// AMIT KUMAR JHA
<window x:class="TestBusyIndicator.Window1" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfx="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
Title="Window1" Height="300" Width="500" Loaded="Window_Loaded">
<grid>
<wpfx:busyindicator name="BusyBar" margin="134,218,135,0" busycontent="Plese wait. " xmlns:wpfx="#unknown">
<listbox height="148" horizontalalignment="Left" margin="110,64,0,0" name="listBox1" verticalalignment="Top" width="281" itemssource="{Binding}">
-------------------------Code-----------------------------
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace TestBusyIndicator
{
public partial class Window1 : Window
{
DispatcherTimer dispatcherTimer = new DispatcherTimer();
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 2);
}
private void StartButton_Click(object sender, RoutedEventArgs e)
{
dispatcherTimer.Start();
BusyBar.IsBusy = true;
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
listBox1.Items.Add(DateTime.Now.Hour.ToString() + ":" +
DateTime.Now.Second.ToString());
CommandManager.InvalidateRequerySuggested();
listBox1.Items.MoveCurrentToLast();
listBox1.SelectedItem = listBox1.Items.CurrentItem;
listBox1.ScrollIntoView(listBox1.Items.CurrentItem);
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
dispatcherTimer.Stop();
BusyBar.IsBusy = false;
}
public void SavePatient()
{
}
}
}
使用下面的程序集之一作为后台工作程序
http://wpftoolkit.codeplex.com/wikipage?title=BusyIndicator [ ^ ]
< a href =http://www.c-sharpcorner.com/UploadFile/mahesh/wpf-busyindicator/> http://www.c-sharpcorner.com/UploadFile/mahesh/wpf-busyindicator/ [ ^ ]
Use one of below assembly as background worker
http://wpftoolkit.codeplex.com/wikipage?title=BusyIndicator[^]
http://www.c-sharpcorner.com/UploadFile/mahesh/wpf-busyindicator/[^]
这篇关于我想在应用程序繁忙时在我的wpf应用程序中显示加载图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!