Some manager read something or did a shitty experiment on his own that said using Linq decreased performance by 0.0005% , and so "ZOMG We need to stop using Linq!"
:30679796
public int testValue = 0;
public MainWindow()
{
InitializeComponent();
Task t = new Task(() =>
{
testValue = 1;
});
t.Start();
// this.Dispatcher.Invoke(t.Start);
}
@TravisJ Yea I see that one works. let me just write up another example of where it fails to work quick.
public int testValue = 0;
public MainWindow()
{
InitializeComponent();
Task t = new Task(() =>
{
for (int x=0;x<1000;x++)
{
var margin = RectObj.Margin; // throws error "The calling thread cannot access this object because a different thread owns it."
margin.Left++;
RectObj.Margin = margin;
Thread.Sleep(10);
}
});
Ok there. from a wpf. I'm just trying to move the margin. But cant access the object from another thread. How do I fix this?
@TravisJ This works now, but is this how it's meant to look? I feel like I'm doing something wrong here or not the proper way
public int testValue = 100;
public MainWindow()
{
InitializeComponent();
Task t = new Task(() =>
{
for (int x = 0; x < 100; x++)
{
this.Dispatcher.Invoke((() =>
{
var margin = RectObj.Margin;
margin.Left = testValue++;
RectObj.Margin = margin;
}));
Thread.Sleep(10);
Ignoring the logic of what I'm doing, and focusing more on the structure of that code. Looks a little odd. creating a task using a lambda expression, then invoking a method by using another lambda expression.
Yea thread.sleep is fine. That was just to see if it was working. Coz I tried using an action which is pointless considering it ran on the same thread anyway.
public int testValue = 100;
public MainWindow()
{
InitializeComponent();
this.Execute().FireAndForget();
}
public async Task Execute()
{
for (int x = 0; x < 100; x++)
{
var margin = RectObj.Margin;
margin.Left = testValue++;
RectObj.Margin = margin;
await Task.Delay(10);
}
}
Sorry. I just learnt about this stuff this week. I dont see what exactly that's supposed to do? Any site that explains that stuff? Just in an overview. no in depth detail needed