I have a web form in silverlight.
On action of some button click i want to update a another control like chart, Textbox etc
In the mean time when it fills the chart or textbox, i need to show a busy indicator
- Busy indicator should show first in screen
- behind the chart value should get update
- Chart value will reflect in screen
- Busy indicator should hide
But the problem is when i try using Thread I am getting an error “Invalid cross-thread access.”. As the thread is accessing
the UI control. The 4 steps which i have tried is as follows.
Any valuable suggestion how to solve this issue.
Step 1 => Tried Thread
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.ComponentModel;
using System.Windows.Threading;
namespace SilverlightApplication2
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void test1()
{
for (int i = 1; i < 10000; i++)
{
System.Diagnostics.Debug.WriteLine(i.ToString());
}
textbox1.Text="test"; //=> Throwing Error "Invalid cross-thread access."
busyIndicator1.IsBusy = false; //=> Throwing Error "Invalid cross-thread access."
}
private void button1_Click(object sender, RoutedEventArgs e)
{
busyIndicator1.IsBusy = true;
Thread th1 = new Thread(test1);
th1.Start();
}
}
}
step 2 => Tried BackgroundWorker
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.ComponentModel;
using System.Windows.Threading;
namespace SilverlightApplication2
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void test1()
{
for (int i = 1; i < 10000; i++)
{
System.Diagnostics.Debug.WriteLine(i.ToString());
}
textbox1.Text="test"; //=> Throwing Error "Invalid cross-thread access."
busyIndicator1.IsBusy = false; //=> Throwing Error "Invalid cross-thread access."
}
private void button1_Click(object sender, RoutedEventArgs e)
{
busyIndicator1.IsBusy = true;
var bw = new BackgroundWorker();
bw.DoWork += (s, args) =>
{
test1(); //Stuff that takes some time
};
bw.RunWorkerCompleted += (s, args) =>
{
busyIndicator1.IsBusy = false;
};
bw.RunWorkerAsync();
}
}
}
Step 3 => Tried to raise an Event from the code behind
public delegate void LinkToEventHandler();
public static event LinkToEventHandler Evt;
private void button1_Click(object sender, RoutedEventArgs e)
{
busyIndicator1.IsBusy = true;
Evt += new LinkToEventHandler(this.test1);
Evt();
}
private void test1()
{
for (int i = 1; i < 10000; i++)
{
System.Diagnostics.Debug.WriteLine(i.ToString());
}
textbox1.Text="test"; //=> Throwing Error "Invalid cross-thread access."
busyIndicator1.IsBusy = false; //=> Throwing Error "Invalid cross-thread access."
}
step 4 => Tried Binding the Busyindicator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.ComponentModel;
using System.Windows.Threading;
namespace SilverlightApplication2
{
public partial class MainPage : INotifyPropertyChanged
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
public const string IsBusyPropertyName = "busyIndicator1";
private bool _isBusy = false;
public bool IsBusy
{
get
{
return _isBusy;
}
set
{
if (_isBusy != value)
{
_isBusy = value;
RaisePropertyChanged(IsBusyPropertyName);
}
}
}
protected void RaisePropertyChanged(string propertyName)
{
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null))
{
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
IsBusy = true;
test1();
IsBusy=false;
}
}
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void test1()
{
for (int i = 1; i < 10000; i++)
{
System.Diagnostics.Debug.WriteLine(i.ToString());
}
}
}
}
Note:- I want a solution which should work in Web form in silverlight not the windows form
This should work for you. Essentially place your processing code in a thread and get back your UI control via a Dispatcher