Note that there are some explanatory texts on larger screens.

plurals
  1. POInvalid cross-thread access. error
    primarykey
    data
    text
    <p>I have a web form in silverlight.</p> <p>On action of some button click i want to update a another control like chart, Textbox etc</p> <p>In the mean time when it fills the chart or textbox, i need to show a busy indicator</p> <ol> <li>Busy indicator should show first in screen</li> <li>behind the chart value should get update</li> <li>Chart value will reflect in screen</li> <li>Busy indicator should hide</li> </ol> <p>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.</p> <p>Any valuable suggestion how to solve this issue.</p> <h2>Step 1 => Tried Thread</h2> <pre><code>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 &lt; 10000; i++) { System.Diagnostics.Debug.WriteLine(i.ToString()); } textbox1.Text="test"; //=&gt; Throwing Error "Invalid cross-thread access." busyIndicator1.IsBusy = false; //=&gt; Throwing Error "Invalid cross-thread access." } private void button1_Click(object sender, RoutedEventArgs e) { busyIndicator1.IsBusy = true; Thread th1 = new Thread(test1); th1.Start(); } } } </code></pre> <h2>step 2 => Tried BackgroundWorker</h2> <pre><code>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 &lt; 10000; i++) { System.Diagnostics.Debug.WriteLine(i.ToString()); } textbox1.Text="test"; //=&gt; Throwing Error "Invalid cross-thread access." busyIndicator1.IsBusy = false; //=&gt; 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) =&gt; { test1(); //Stuff that takes some time }; bw.RunWorkerCompleted += (s, args) =&gt; { busyIndicator1.IsBusy = false; }; bw.RunWorkerAsync(); } } } </code></pre> <h2>Step 3 => Tried to raise an Event from the code behind</h2> <pre><code>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 &lt; 10000; i++) { System.Diagnostics.Debug.WriteLine(i.ToString()); } textbox1.Text="test"; //=&gt; Throwing Error "Invalid cross-thread access." busyIndicator1.IsBusy = false; //=&gt; Throwing Error "Invalid cross-thread access." } </code></pre> <h2>step 4 => Tried Binding the Busyindicator</h2> <pre><code>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 &lt; 10000; i++) { System.Diagnostics.Debug.WriteLine(i.ToString()); } } } } </code></pre> <p>Note:- I want a solution which should work in Web form in silverlight not the windows form</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload