WPF : I got problem in draw something on canvas using thread - wpf

I try to draw something on canvas every 1 second .Then, I create a thread to call a method to draw after sleep 1 second. The problem is calling Canvas object to draw. The program tell me error message
The calling thread cannot access this object because a different thread owns it.
Code
private void drawTimeTick() {
...
Thread iThread = new Thread(new ThreadStart(tickThread));
iThread.Start();
}
private void tickThread(){
try
{
Thread.Sleep(1000);
...
Canvas.SetLeft(tick, 700);
Canvas.SetTop(tick, 30);
}catch(Exception ex){
MessageBox.Show("Exception tickThread : "+ex.Message);
}
}
How to access Canvas in Thread to draw?

Have a look at the DispatcherTimer class. It is designed for exactly this scenario, as can be seen in the MSDN examples.

You can call
this.Dispatcher.Invoke((Action)(() =>
{
Canvas.SetLeft(tick, 700);
Canvas.SetTop(tick, 30);
}));
(I'm assuming that this is some UI control. If not you can use Application.Current.Dispatcher instead). It will execute your canvas operations on the UI thread.

I have try DispatcherTimer, It's work.
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += tickThread;
timer.Start();
}
private void tickThread(object sender, EventArgs e)
{
try
{
tick.thisTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
double TickPixel = GetPixel(ClassTimeline.Width, startTime, endTime, tick.thisTime);
Canvas.SetLeft(tick, TickPixel);
Canvas.SetTop(tick, 30);
}catch(Exception ex){
MessageBox.Show("Exception tickThread : "+ex.Message);
}
}

You can't access graphical objects from a different thread from the ones that created it.

Related

run multiple tasks in parallel using C#.net 2.0

I need to perform 2 tasks in parallel. One will load data in the GUI, till then I want to run a progress bar continuously in front of user. I tried BackgroundWorker but it is giving me some Thread synchronization error. Can somebody suggest me any other best way of doing same.
Code:
backgroundWorker1 initialization:
backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
if (backgroundWorker1.IsBusy != true)
{
backgroundWorker1.RunWorkerAsync();
}
error coming on following line:
XmlDocumentHierarchy _remoteObj = new XmlDocumentHierarchy(comboBox2.Text, "username", "password");
is:
"Cross-thread operation not valid: Control 'comboBox2' accessed from a thread other than the thread it was created on."
You are trying access comboBox2.Text in thread other than GUI thread (background worker thread). If you using only one property in background worker thread, than you can pass `comboBox2.Text' to background worker method:
if (backgroundWorker1.IsBusy != true)
{
backgroundWorker1.RunWorkerAsync(comboBox2.Text);
}
In backgroundWorker1_DoWork procedure you can read property in following way:
void backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e)
{
String comboBoxText = (String)e.Argument;
XmlDocumentHierarchy _remoteObj = new XmlDocumentHierarchy(comboBoxText, "username", "password");
}
If you accessing more than one property from GUI controls you can create simple class to pass all necessary data to your background worker method.
If you need to access GUI thread from BackgroundWorker thread, you can easily invoke your methods in the GUI thread like this:
public Form1()
{
InitializeComponent();
Thread thr = new Thread(new ThreadStart(BackGroundThread));
thr.Start();
}
void BackGroundThread()
{
for (int i = 0; i < 100; i++)
{
// The line below will be run in the GUI thread with no synchronization issues
BeginInvoke((Action)delegate { this.Text = "Processed " + i.ToString() + "%"; });
Thread.Sleep(200);
}
}

Update GUI using BackgroundWorker

I've been searching and found that a good way to perform background work and update the GUI is using background workers. However, doing this (stupid) little task (counting from 1 to 10000) it doesn't update the label content but prints to the debug! (This is just a spike solution for another project of course...)
Here's the code:
public partial class MainWindow : Window
{
BackgroundWorker bw = new BackgroundWorker();
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.WorkerReportsProgress = true;
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.RunWorkerAsync();
}
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("DONE");
}
void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Content = "going here: "+e.ProgressPercentage;
Debug.WriteLine(e.ProgressPercentage);
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
for (int i=0; i < 10000; i++)
{
bw.ReportProgress((i*100)/10000);
}
}
}
The ProgressChanged event is raised on the UI thread, not the worker thread. In your code, the worker thread is doing almost nothing (just loop from 0 to 10000 and call ReportProgress), most of the work is done on the UI thread. Basically, you're sending too many progress notifications. Because of this, the UI thread is almost always busy and has no time to render the new content of the label.
Rendering in WPF is not performed immediately when you change a property of a control, it is done on a separate dispatcher frame, which is processed when the dispatcher has nothing more urgent to do, based on the priority of the task. The priority used for rendering has a value of 7 (DispatcherPriority.Render); the ProgressChanged event is marshalled to the UI thread with a priority of 9 (DispatcherPriority.Normal), as specified on MSDN. So the ProgressChanged notifications always have a higher priority than rendering, and since they keep coming, the dispatcher never has time to process the rendering tasks.
If you just decrease the frequency of the notifications, your app should work fine (currently you're sending 100 notifications for each percentage value, which is useless):
void bw_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 10000; i++)
{
if (i % 100 == 0)
bw.ReportProgress(i / 100);
}
}
this.Dispatcher.BeginInvoke( (Action) delegate(){
label1.Content = "going here: "+e.ProgressPercentage;
});
Try to change the label using womething like this:
string Text = "going here: " + e.ProgressPercentage;
this.Invoke((MethodInvoker)delegate {
label1.Content = newText;
});
Note that i'm not sure it will work. I can not test it now. If it does not work, let me know and I will delete the answer.
If you need the a canonical way to do exactly what you want, look at the Hath answer in this post: How do I update the GUI from another thread?

System.ComponentModel.BackgroundWorker never invokes ProgressChanged

I recently rewrote my Windows Forms application to use BackgroundWorker instances instead of using manually created "worker threads". After checkin I noticed some tests started to fail. After some debugging I can demonstate my problems by showing you the following 2 tests:
[Test]
public void Test_A()
{
bool progressChanged = false;
var worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += (s, e) => worker.ReportProgress(0, null);
worker.ProgressChanged += (s, e) => progressChanged = true;
worker.RunWorkerAsync();
Thread.Sleep(100);
progressChanged.ShouldBeTrue();
}
[Test]
public void Test_B()
{
//Creation of o form component causes (?) this test to fail, even do I dispose it
var view = new Form();
view.Dispose();
bool progressChanged = false;
var worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += (s, e) => worker.ReportProgress(0, null);
worker.ProgressChanged += (s, e) => progressChanged = true;
worker.RunWorkerAsync();
Thread.Sleep(100);
progressChanged.ShouldBeTrue();
}
Test_A success while Test_B fails. This is regardless if I sleep 100 ms or 100 minutes. Why?? My production code seem to work though, but it is annoying not being able to have a regression test suite that works (I have other test creating Forms components and these tests must be executed BEFORE my test using BackgroundWorker)
The next step would be to examine the source code of BackgroundWorker, but before I do that I thought I`d check for help here.
Regards Michael
Add
WindowsFormsSynchronizationContext.AutoInstall = false;
In Test_B() before anything else. The BackgroundWorker, wich is very UI (ie: Winforms) oriented, is wild guessing how to sync things, but not like you want. Have a look here for some explanation (especially from Scott Berry): WindowsFormsSynchronizationContext

WPF, calling server method in background worker

I need in wpf app check messages on server. I have own method which load messages on server-LoadRp().
I would like to create some kind of listener which would check, every 3 seconds whether on the server are not new messages.
I call method for loading messages on dispatcher timer tick event, it is suitable? Any another solution. It’s possible call timer in another thread in wpf?
Code is here:
public MessangerWindow(PokecCommands pokecCmd)
{
InitializeComponent();
PokecCmd = pokecCmd;
_friendsData = PokecCmd.LoadFriends();
friendsListBox.DataContext = _friendsData;
_dispatcherTimer = new DispatcherTimer();
_dispatcherTimer.Tick+=new EventHandler(DispatcherTimer_Tick);
_dispatcherTimer.Interval = new TimeSpan(0,0,3);
_dispatcherTimer.Start();
}
private void DispatcherTimer_Tick(object sender, EventArgs eventArgs)
{
try
{
//try load new message from sever
RP message = PokecCmd.LoadRp();
//arived message
if (message != null)
{
//exist window
if (_chatWindows.ContainsKey(message.Nick))
{
_chatWindows[message.Nick].Show();
}
{
//create new Window
var chatWindow = new ChatWindow(PokecCmd, message);
_chatWindows.Add(message.Nick, chatWindow);
chatWindow.Show();
}
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
}
What is suitable to use:
Dispatcher with no background threads
Dispatcher with background threads
Multiple Threads
If you are ok with locking up your UI for the time it takes to check on the server, using a DispatcherTimer the way you are doing it will work fine.
If checking for new messages could take more than a few milliseconds and you want your UI to be responsive while it checks, you should use multiple threads. In that case, once the new data had arrived you would use Dispatcher.Invoke to display it.
Your code in the thread that checks for messages might look like this:
//try load new message from sever
RP message = PokecCmd.LoadRp();
//arived message
if( message != null )
Dispatcher.Invoke(DispatcherPriority.Send, new Action(() =>
{
//exist window
if (_chatWindows.ContainsKey(message.Nick))
{
_chatWindows[message.Nick].Show();
}
{
//create new Window
var chatWindow = new ChatWindow(PokecCmd, message);
_chatWindows.Add(message.Nick, chatWindow);
chatWindow.Show();
}
}
);

WPF System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it

In my Windows I have a TextBox which I like to update (text property) from another thread. When doing so, I get the InvalidOperationException (see title). I have found different links in google explaining this, but I still can't seem to make it work.
What I've tried is this:
Window1 code:
private static Window1 _myWindow;
private MessageQueueTemplate _messageQueueTemplate;
private const string LocalTemplateName = "LocalExamSessionAccessCodeMessageQueueTemplate";
private const string RemoteTemplateName = "RemoteExamSessionAccessCodeMessageQueueTemplate";
...
public Window1()
{
InitializeComponent();
_myWindow = this;
}
public static Window1 MyWindow
{
get
{
return _myWindow;
}
}
public void LogText(string text)
{
informationTextBox.Text += text + Environment.NewLine;
}
...
In another class (actually a spring.NET Listener adapter, listening to a certain queue, started in another thread).
var thread = new Thread(
new ThreadStart(
delegate()
{
Window1.MyWindow.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate()
{
Window1.MyWindow.LogText(text);
}
));
}
));
It doesn't throw an error, but the text in the LogText method in Window1 isn't triggered, so the text isn't updated.
So basically, I want to update this TextBox component from another class running in another thread.
Window1.MyWindow.informationTextBox.Dispatcher.Invoke(
DispatcherPriority.Normal,
new Action(() => Window1.MyWindow.informationTextBox.Text += value));
Well, using Dispatcher.Invoke or BeginInvoke is definitely the way to go, but you haven't really shown much code other than creating a thread - for example, you haven't started the thread in your second code block.
If you put the Dispatcher.Invoke code in the place where previously you were getting an InvalidOperationException, it should be fine.
For WPF, I find this construct:
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += ( s, e ) =>
{
};
bw.RunWorkerCompleted += ( s, e ) =>
{
};
bw.RunWorkerAsync();
to be the most useful. The RunWorkerCompleted block will typically update an ObservableCollection or fire off a RaisePropertyChangedEvent.

Resources