I want to move a rectangle in WPF application using the following code. However, I am getting the following error:
System.InvalidOperationException: Cannot use a DependencyObject that belongs to a different thread
I looked at other problems in stackoverflow but nothing worked.
public partial class MainWindow : Window
{
private Rectangle rect;
int count = 1;
Timer timer;
public MainWindow()
{
InitializeComponent();
Rectangle movedRectangle = new Rectangle();
movedRectangle.Width = 200;
movedRectangle.Height = 50;
movedRectangle.Fill = Brushes.Blue;
movedRectangle.Opacity = 0.5;
TranslateTransform translateTransform1 = new TranslateTransform(50, 20);
movedRectangle.RenderTransform = translateTransform1;
this.can.Children.Add(movedRectangle);
this.rect = movedRectangle;
timer = new Timer(500);
timer.Elapsed += OnTimedEvent;
timer.Enabled = true;
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
count++;
TranslateTransform translateTransform1 = new TranslateTransform(50 + count * 2, 20);
this.rect.Dispatcher.Invoke(new Action(()=>
rect.RenderTransform = translateTransform1));
//this.can.UpdateLayout();
this.can.Dispatcher.Invoke(new Action(()=>
this.can.UpdateLayout()
));
}
I would suggest you to use DispatcherTimer than a normal timer.
Please see the below solution. enjoy.
Note: for DispatcherTimer you will need to add the assembly reference for System.Windows.Threading
public partial class MainWindow : Window
{
private Rectangle rect;
int count = 1;
private DispatcherTimer timer = null;
public MainWindow()
{
InitializeComponent();
Rectangle movedRectangle = new Rectangle();
movedRectangle.Width = 200;
movedRectangle.Height = 50;
movedRectangle.Fill = Brushes.Blue;
movedRectangle.Opacity = 0.5;
TranslateTransform translateTransform1 = new TranslateTransform(50, 20);
movedRectangle.RenderTransform = translateTransform1;
this.can.Children.Add(movedRectangle);
this.rect = movedRectangle;
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 0, 0, 500);
timer.Tick += timer_Tick;
timer.Start();
timer.IsEnabled = true;
}
void timer_Tick(object sender, EventArgs e)
{
count++;
TranslateTransform translateTransform1 = new TranslateTransform(50 + count * 2, 20);
Dispatcher.BeginInvoke(new Action<TranslateTransform>(delegate(TranslateTransform t1)
{
rect.RenderTransform = t1;
this.can.UpdateLayout();
}), System.Windows.Threading.DispatcherPriority.Render, translateTransform1);
}
}
You are constructing a TranslateTransform (which is a DependencyObject) outside the UI thread. Easy fix:
this.rect.Dispatcher.Invoke(new Action(
() =>
{
TranslateTransform translateTransform1 = new TranslateTransform(50 + count * 2, 20);
rect.RenderTransform = translateTransform1;
}));
Arguably a better fix: use a DispatcherTimer instead and get rid of all your Dispatcher.Invoke calls.
Related
I have been trying to run a very simple application that moves a 20 by 20 pixel square 20 pixels to the right on a canvas every second. I am using a dispatchertimer to fire the event every second.
The problem is that the square doesn't move to the right unless I shake the application window (with my mouse), and it occasionally moves on its own (albeit not every second).
I have already tried reinstalling Visual Studio 2017 and installing it on my SSD and HDD, neither seem to fix the issue.
Here is the full code of the application's MainWindow.xaml.cs
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DispatcherTimer timer = new DispatcherTimer();
Rectangle s = new Rectangle();
Point currentPosition = new Point(20, 20);
public MainWindow()
{
InitializeComponent();
timer.Tick += Timer_Tick;
timer.Interval = TimeSpan.FromSeconds(1);
timer.Start();
s.Width = 20;
s.Height = 20;
s.Fill = new SolidColorBrush(Colors.Black);
map.Children.Add(s);
}
public void Timer_Tick(object sender, EventArgs e)
{
RedrawSquare();
}
public void RedrawSquare()
{
map.Children.Clear();
s.Width = 20;
s.Height = 20;
s.Fill = new SolidColorBrush(Colors.Black);
Canvas.SetLeft(s, currentPosition.X += 20);
map.Children.Add(s);
}
}
On the MainWindow.xaml file there is an empty Canvas with the name "map"
Thank you in advance
You don't need to remove and add the Rectangle on each timer tick, or reset its properties each time.
Just increment the value of the Canvas.Left property:
public partial class MainWindow : Window
{
private readonly DispatcherTimer timer = new DispatcherTimer();
private readonly Rectangle s = new Rectangle();
public MainWindow()
{
InitializeComponent();
timer.Tick += Timer_Tick;
timer.Interval = TimeSpan.FromSeconds(1);
timer.Start();
s.Width = 20;
s.Height = 20;
s.Fill = Brushes.Black;
Canvas.SetLeft(s, 0);
map.Children.Add(s);
}
public void Timer_Tick(object sender, EventArgs e)
{
Canvas.SetLeft(s, Canvas.GetLeft(s) + 20);
}
}
The movement would however be much smoother with an animation:
public MainWindow()
{
InitializeComponent();
s.Width = 20;
s.Height = 20;
s.Fill = Brushes.Black;
Canvas.SetLeft(s, 0);
map.Children.Add(s);
var animation = new DoubleAnimation
{
By = 20,
Duration = TimeSpan.FromSeconds(1),
IsCumulative = true,
RepeatBehavior = RepeatBehavior.Forever
};
s.BeginAnimation(Canvas.LeftProperty, animation);
}
You can try setting the DispatcherPriority to Normal.
Instantiate your timer like this:
DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal);
EDIT:
Although this somehow fixed the issue (square was moving without the need to move the window), it's apparently still the wrong answer. I don't know much about the DispatcherTimer, but I recall having changed the priority once but I don't remember why. In any case, it might be helpful to someone else.
I have created a PlayPage.xaml, PlayPage.xaml.cs and Game.cs file.
The PlayPage.xaml.cs has two variable, windowWidth and windowHeight.
I would like access two public static variable from Game.cs.
Game.cs:
namespace UwpApp
{
class Game
{
static Rectangle rectangle;
PlayPage pg = new PlayPage();
//Create a rectangle
public Rectangle draw()
{
rectangle = new Rectangle();
rectangle.Width = 70;
rectangle.Height = 70;
rectangle.Fill = new SolidColorBrush(Colors.Green);
Canvas.SetLeft(rectangle, randPos());
Canvas.SetTop(rectangle, randPos());
return rectangle;
}
//Create a random X and Y position
private Int32 randPos()
{
Random rnd = new Random();
Debug.WriteLine(pg.windowWidth);
return rnd.Next(0 , (int)pg.windowWidth);
}
}
}
PlayPage.xaml.cs:
namespace UwpApp
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public partial class PlayPage : Page
{
DispatcherTimer timer;
Rectangle rect;
bool isTapped;
public static double windowWidth, windowHeight;
Game game = new Game();
public PlayPage()
{
this.InitializeComponent();
startCounter.Visibility = Visibility.Collapsed;
isTapped = false;
}
private void Load_Variable(object sender, RoutedEventArgs e)
{
windowWidth = canvas.ActualWidth;
windowHeight = canvas.ActualHeight;
}
//Counter animation. (Number opacity, fall)
private void counterAnimation()
{
startCounter.Visibility = Visibility.Visible;
//Set Counter Horizontal Align
double left = (canvas.ActualWidth - startCounter.ActualWidth) / 2;
Canvas.SetLeft(startCounter, left);
Storyboard storyboard = new Storyboard();
DoubleAnimation opacityAnim = new DoubleAnimation();
DoubleAnimation fallAnim = new DoubleAnimation();
opacityAnim.From = 1;
opacityAnim.To = 0;
opacityAnim.Duration = new Duration(TimeSpan.FromSeconds(1));
opacityAnim.AutoReverse = false;
opacityAnim.RepeatBehavior = new RepeatBehavior(3);
Storyboard.SetTarget(opacityAnim, this.startCounter);
Storyboard.SetTargetProperty(opacityAnim, "(UIElement.Opacity)");
fallAnim.From = -115;
fallAnim.To = canvas.ActualHeight / 2;
fallAnim.Duration = new Duration(TimeSpan.FromSeconds(1));
fallAnim.AutoReverse = false;
fallAnim.RepeatBehavior = new RepeatBehavior(3);
Storyboard.SetTarget(fallAnim, this.startCounter);
Storyboard.SetTargetProperty(fallAnim, "(Canvas.Top)");
storyboard.Children.Add(opacityAnim);
storyboard.Children.Add(fallAnim);
storyboard.Begin();
}
//Countdown Timer
private void countDown()
{
timer = new DispatcherTimer();
timer.Tick += startCounter_CountDown;
timer.Interval = new TimeSpan(0, 0, 1);
timer.Start();
}
//Change Countdown value
private void startCounter_CountDown(object sender, object e)
{
int counterNum = int.Parse(startCounter.Text);
counterNum -= 1;
startCounter.Text = counterNum.ToString();
if (counterNum == 1)
{
timer.Stop();
StartedGame();
}
}
//Tap or Click to start the game
private void TapToStart(object sender, TappedRoutedEventArgs e)
{
if(!isTapped)
{
isTapped = true;
counterAnimation();
countDown();
this.TapToStartText.Visibility = Visibility.Collapsed;
}
}
//Create rectangles
private void StartedGame()
{
rect = game.draw();
canvas.Children.Add(game.draw());
Debug.WriteLine(windowWidth.ToString());
}
}
}
One more thing: I get an error this line: PlayPage pg = new PlayPage();
Error (pic)
The error in your picture is due to infinite recursion, as it says. The constructor for Game instantiates a new PlayPage. The constructor for PlayPage instantiates a new Game. Which instantiates a new PlayPage. Which instantiates a new Game. and on and on.
Static members are accessed by class name, not by instance. Like so:
PlayPage.windowWidth
I had created multiple timers using for loop and the timers are diplayed in the corresponding labels.If i click the button "n" times then "n" times ll be created.This code shows error in time countdown during runtime. Each timer is getting decremented by different intervals.How to resolve this problem?
public Dictionary<Timer, Label> dict = new Dictionary<Timer, Label>();
int n = 1;
int timesec = 10;
private void CreateTimers()
{
for (int i = 1; i <= n; i++)
{
Timer timer = new Timer();
timer = new System.Windows.Forms.Timer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = (1000);//1 sec
Label label = new Label();
label.Name = "label" + i;
label.Location = new Point(0, 100 + i * 30);
label.TabIndex = i;
label.Visible = true;
this.Controls.Add(label);
dict[timer] = label;
timer.Enabled = true;
timer.Start();
}
private void button2_Click(object sender, EventArgs e)
{
//function call
CreateTimers();
n++;
}
private void timer_Tick(object sender, EventArgs e)
{
//timer countdown
Timer t = (Timer)sender;
timesec--;
if (timesec == 0)
t.Stop();
dict[t].Text = timesec.ToString();
}
A couple of issues. First and foremost, you have to eliminate that loop, since that creates more timers than you probably realize. Also, since you want each timer to be independent, you can't change the timesec value.
Try using the value of the label to show the count down, something like this:
private void CreateTimer() {
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Tick += timer_Tick;
timer.Interval = (1000);//1 sec
Label label = new Label();
label.Name = "label" + n.ToString();
label.Text = timesec.ToString();
label.Location = new Point(0, 100 + n * 30);
label.Visible = true;
this.Controls.Add(label);
dict.Add(timer, label);
timer.Enabled = true;
timer.Start();
n++;
}
private void timer_Tick(object sender, EventArgs e) {
System.Windows.Forms.Timer t = (System.Windows.Forms.Timer)sender;
Label l = dict[t];
int labelTime = 0;
if (Int32.TryParse(l.Text, out labelTime)) {
labelTime -= 1;
}
l.Text = labelTime.ToString();
if (labelTime == 0) {
t.Stop();
}
}
I have Label in Panel. In timer.Tick new value write in Label.Text, but in form not change.
Program.cs:
static void Main(string[] args)
{
MainForm mainForm = new MainForm();
Engine engine = new Engine(mainForm);
Application.Run(mainForm);
}
MainForm.cs
public partial class MainForm : Form
{
public MatchesPanel panel;
public MainForm()
{
InitializeComponent();
this.Load += MainForm_Load;
this.panel = new MatchesPanel("panel", new System.Drawing.Point(0, 52));
this.panel.Name = "panel";
this.panel.TabIndex = 8;
this.Controls.Add(this.panel);
}
}
MatchesPanel.cs
public class MatchesPanel : Panel
{
public string Name;
int VWheelSize = 0;
Dictionary<string, Label> Items = new Dictionary<string, Label>();
Label InFocus = null;
public MatchesPanel(string name, Point location)
{
Name = name;
this.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.Location = location;
this.Margin = new System.Windows.Forms.Padding(0);
this.Size = new System.Drawing.Size(400, 323);
this.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.MouseWheel += This_MouseWheel;
this.Click += This_Click;
this.Invalidated += MatchesPanel_Invalidated;
}
void Label_Click(object s, EventArgs e)
{
Label l = (Label)s;
l.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
l.BackColor = System.Drawing.Color.MediumTurquoise;
if (InFocus != null && InFocus != l)
{
InFocus.BorderStyle = System.Windows.Forms.BorderStyle.None;
InFocus.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
}
InFocus = l;
l.Focus();
}
public void SetText(string key, string text)
{
Label l = Items[key];
l.Text = text;
}
public void Add(string key, string text)
{
Label label = new Label();
label.AllowDrop = true;
label.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
label.Font = new System.Drawing.Font("Consolas", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
label.Location = new Point(0, Items.Count * 37 + VWheelSize);
label.Margin = new System.Windows.Forms.Padding(0);
label.Padding = new System.Windows.Forms.Padding(3);
label.Size = new System.Drawing.Size(400, 37);
label.Text = text;
label.Click += Label_Click;
label.LostFocus += Label_LostFocus;
label.MouseLeave += Label_MouseLeave;
label.MouseMove += Label_MouseMove;
this.Controls.Add(label);
Items.Add(key, label);
}
public void UpdateMatches(Dictionary<string, Match> Matches)
{
string newStr;
// calculate newStr...
// in debugger i check newStr value and this right
// but text not updating
this.SetText(key, newStr);
}
}
Engine.cs
public class Engine
{
System.Windows.Forms.Timer timer = null;
public Dictionary<string, Match> Matches;
MainForm MainForm = null;
public Engine(MainForm mainForm)
{
MainForm = mainForm;
Matches = new Dictionary<string, Match>();
timer = new System.Windows.Forms.Timer();
timer.Tick += timer_Tick;
timer.Interval = 1000;
timer.Start();
}
public void timer_Tick(object s, EventArgs e)
{
System.Windows.Forms.Timer timer = (System.Windows.Forms.Timer)s;
timer.Stop();
Proc();
timer.Start();
}
public async void Proc()
{
// do something...
MainForm.Invoke((System.Windows.Forms.MethodInvoker)delegate()
{
MainForm.panel.UpdateMatches(Matches);
});
}
}
I try use:
Label.Update();
Label.Refresh();
Label.Invalidate();
but it not work.
If I click in label, when it not in focus (InFocus != sourceLabel in clickHandler), text value updating in sourceLabel one time.
Help me pls. I read another topics and not find solve.
If need more code, tell me.
Thx.
[EDIT]
I simplified my code.
Program.cs:
static void Main(string[] args)
{
MainForm mainForm = new MainForm();
Engine engine = new Engine(mainForm);
Application.Run(mainForm);
}
MatchesPanel.cs
public class MatchesPanel : Panel
{
Dictionary<string, Label> Items = new Dictionary<string, Label>();
public MatchesPanel(Point location)
{
this.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.Location = location;
this.Margin = new System.Windows.Forms.Padding(0);
this.Size = new System.Drawing.Size(400, 323);
this.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
}
public void SetText(string key)
{
Label l = Items[key];
l.Text = DateTime.Now.ToString();
}
public void Add(string key)
{
Label label = new Label();
label.Name = key;
label.AllowDrop = true;
label.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
label.Font = new System.Drawing.Font("Consolas", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
label.Location = new Point(0, Items.Count * 37 + VWheelSize);
label.Margin = new System.Windows.Forms.Padding(0);
label.Padding = new System.Windows.Forms.Padding(3);
label.Size = new System.Drawing.Size(400, 37);
label.Text = DateTime.Now.ToString();
this.Controls.Add(label);
Items.Add(key, label);
}
}
Engine.cs
public class Engine
{
System.Windows.Forms.Timer timer = null;
public MatchesPanel panel = null;
MainForm MainForm = null;
public Engine(MainForm mainForm)
{
MainForm = mainForm;
timer = new System.Windows.Forms.Timer();
timer.Tick += timer_Tick;
timer.Interval = 1000;
timer.Start();
}
public void timer_Tick(object s, EventArgs e)
{
System.Windows.Forms.Timer timer = (System.Windows.Forms.Timer)s;
timer.Stop();
MainForm.Invoke((System.Windows.Forms.MethodInvoker)delegate()
{
if (panel == null)
{
panel = new MatchesPanel(new System.Drawing.Point(0, 52));
panel.Name = "key1";
// add label in main form
panel.Add("key1");
MainForm.Controls.Add(panel);
}
panel.SetText("key1");
});
timer.Start();
}
}
Problem:
Label.Text not updating.
Solve:
public class UpdatableLabel : Button
{
public UpdatableLabel() : base()
{
FlatAppearance.BorderSize = 0;
FlatStyle = System.Windows.Forms.FlatStyle.Flat;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Pen pen = new Pen(FlatAppearance.BorderColor, 1);
Rectangle rectangle = new Rectangle(0, 0, Size.Width - 1, Size.Height - 1);
e.Graphics.DrawRectangle(pen, rectangle);
}
}
Now my "label" is updated.
I have a canvas where I draw rectangles and move them randomly with the help of a storyboard. After a few cycles the storyboard.completed event does not fire any more. Does anybody know? here is my xaml:
<Grid>
<Canvas Name="movingCanvas" Background="Green" Margin="0,29,0,0"></Canvas>
<TextBlock Height="23" Name="textBlock1" Text="TextBlock" Margin="528,0,0,538" />
</Grid>
And the code:
private Random random = new Random();
private Storyboard gameLoop = new Storyboard();
private int i = 0;
public Window3()
{
InitializeComponent();
this.gameLoop.Duration = TimeSpan.FromMilliseconds(100);
this.gameLoop.Completed += new EventHandler(this.gameLoop_Completed);
this.gameLoop.Begin();
}
private void gameLoop_Completed(object sender, EventArgs e)
{
this.addRectangle();
this.moveRectangle();
i++;
this.textBlock1.Text = i.ToString();
this.gameLoop.Begin();
}
private void addRectangle()
{
Rectangle rect = new Rectangle();
rect.Height = 100;
rect.Width = 100;
rect.Stroke = new SolidColorBrush(Colors.Black);
Canvas.SetLeft(rect, random.Next((int)this.Width));
Canvas.SetTop(rect, random.Next((int)this.Height));
this.movingCanvas.Children.Add(rect);
}
private void moveRectangle()
{
foreach (UIElement elm in this.movingCanvas.Children)
{
int moveLeft = random.Next(10);
int distance = random.Next(-10, 20);
if (moveLeft > 5)
{
Canvas.SetTop(elm, Canvas.GetTop(elm) + distance);
}
else
{
Canvas.SetLeft(elm, Canvas.GetLeft(elm) + distance);
}
}
}
The Completed event does also not occur whitout creating and moving rectangles:
private Storyboard gameLoop = new Storyboard();
private int i = 0;
public Window3()
{
InitializeComponent();
this.gameLoop.Duration = TimeSpan.FromMilliseconds(100);
this.gameLoop.Completed += new EventHandler(this.gameLoop_Completed);
this.gameLoop.Begin();
}
private void gameLoop_Completed(object sender, EventArgs e)
{
i++;
this.textBlock1.Text = i.ToString();
this.gameLoop.Begin();
}
If you add a animation to the storyboard, the storyboard not stops firing the event.
public Window3()
{
InitializeComponent();
this.gameLoop.Duration = TimeSpan.FromMilliseconds(100);
this.gameLoop.Completed += new EventHandler(this.gameLoop_Completed);
DoubleAnimation animation= new DoubleAnimation { From = 100, To = 101 };
ani.SetValue(Storyboard.TargetProperty, this);
ani.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("Height"));
this.gameLoop.Children.Add(ani);
this.gameLoop.Begin();
}
Like Kshitij Mehta said above, i think use a timer instead the storyboard, but maybe you have a reason to use a storyboard....
Do you really mean to add a new rectangle on every iteration of the loop?
You'll very quickly get tens of thousands if not millions of rectangles which will take longer and longer to draw.