UIProgressView not updating from BackgroundWorker.ProgressChanged - c#

I'm in the process of porting my Xamarin.Android app to Xamarin.iOS, I can't make my progress bar update, where am I going wrong?
The values are set in updateProgressBar() correctly and progressBarValue in this example is set as 0.25 as expected, but the UIProgressView is not updated on the screen. progressBar is a UIProgressView on the storyboard.
public BackgroundWorker backgroundWorker { get; private set; }
private float progressBarValue { get; set; }
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
startBackgroundWorker();
}
private void startBackgroundWorker()
{
if (backgroundWorker == null || backgroundWorker.CancellationPending) backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += (s, e) =>
{
//do stuff
backgroundWorker.ReportProgress(25);
//do stuff
};
backgroundWorker.RunWorkerCompleted += (s, e) => { //do stuff };
backgroundWorker.ProgressChanged += (s, e) => { updateProgressBar(e.ProgressPercentage); };
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.RunWorkerAsync();
}
private void updateProgressBar(float v)
{
if (v > 0){
float value = v / 100;
progressBarValue = progressBarValue + value;
if (progressBarValue > 1) progressBarValue = 1;
progressBar.Progress = progressBarValue;
}
}
I also tried using SetProgress(progressBarValue,true)
private void updateProgressBar(float v)
{
if (v > 0){
float value = v / 100;
progressBarValue = progressBarValue + value;
if (progressBarValue > 1) progressBarValue = 1;
progressBar.SetProgress(progressBarValue,true);
}
}
and using InvokeOnMainThread
private void updateProgressBar(float v)
{
if (v > 0){
float value = v / 100;
progressBarValue = progressBarValue + value;
if (progressBarValue > 1) progressBarValue = 1;
InvokeOnMainThread ( () => {
// manipulate UI controls
progressBar.SetProgress(progressBarValue,true);
});
}
}

you need to be sure your UI updates are running on the main thread
InvokeOnMainThread ( () => {
// manipulate UI controls
progressBar.SetProgress(progressBarValue,true);
});

As is often the case, my problem was not related to my code in the question, or the question at all, I copied it into a new solution and it worked fine.
Incase anyone finds this in the future and has made the same mistake:
My problem was that I had set UIProgressView.TintColor to an invalid value elsewhere in my code so the ProgressView was updating all along but it just wasn't visible.
progressView.TintColor = iOSHelpers.GetColor(GenericHelpers.colorPrimaryGreenRGBA);
iOSHelpers
public static UIColor GetColor(int[] rgba)
{
if (rgba.Length == 4)
return UIColor.FromRGBA(rgba[0], rgba[1], rgba[2], rgba[3]);
else return UIColor.Black;
}
GenericHelpers
public static int[] colorPrimaryGreenRGBA = { 140, 185, 50, 1 };
Once I had changed thecolorPrimaryGreenRGBA values to floats and divided the RGB channels by 255 in the GetColor method it was then a visible color.

Related

Why doesn't the method work in the OnMessage event?

I use WebSocketSharp to write a client, in Start I add the ChangeColor method to the OnMessage event, and the method is executed but not to the end, it does not change the color of the object, although it should. I tried to call the method in Update on button click and it worked correctly. What is the problem?
The server sends correct data, I checked
WebSocket ws;
private Renderer objectRenderer;
private Color matColor;
private string reciveData;
private void Start()
{
objectRenderer = GetComponent<Renderer>();
ws = new WebSocket("ws://127.0.0.1:8080");
ws.Connect();
ws.OnMessage += (sender, e) =>
{
reciveData = e.Data;
ChangeColor(e.Data); // its dosent work!
};
}
void Update()
{
if (ws == null)
return;
// its work
// if (Input.GetKeyDown(KeyCode.Space))
// {
// ChangeColor(reciveData);
// objectRenderer.material.color = matColor;
// }
}
public void ChangeColor(string data)
{
string[] colors = data.Split(',');
matColor = new Color()
{
r = float.Parse(colors[0]) / 255.0f,
g = float.Parse(colors[1]) / 255.0f,
b = float.Parse(colors[2]) / 255.0f,
a = float.Parse(colors[3]) / 255.0f
};
objectRenderer.material.color = matColor;
}
It does not work, because the WebSocketSharp callbacks run on a worker thread. Unity does not allow to interact with your objectRenderer in any other thread than the main thread.
You can cache the data as you did and process it in update method (which is called in main thread).
Use a boolean to tell the Update method to call your ChangeColour method.
WebSocket ws;
private Renderer objectRenderer;
private Color matColor;
private string reciveData;
private bool newDataToBeProcessed;
private void Start()
{
objectRenderer = GetComponent<Renderer>();
ws = new WebSocket("ws://127.0.0.1:8080");
ws.Connect();
ws.OnMessage += (sender, e) =>
{
reciveData = e.Data;
newDataToBeProcessed = true;
};
}
void Update()
{
if (ws == null)
return;
if (newDataToBeProcessed == true)
{
newDataToBeProcessed = false;
ChangeColor(reciveData);
objectRenderer.material.color = matColor;
}
}
public void ChangeColor(string data)
{
string[] colors = data.Split(',');
matColor = new Color()
{
r = float.Parse(colors[0]) / 255.0f,
g = float.Parse(colors[1]) / 255.0f,
b = float.Parse(colors[2]) / 255.0f,
a = float.Parse(colors[3]) / 255.0f
};
objectRenderer.material.color = matColor;
}

Progress bar is jumping

When I start program, I need to load some projects and init them. So I make that all in Thread and create a progress bar (next PB). When loading is started everythink is OK, but in one moment PB is stop and jump to end when init will end.
Here Thread which add to progress bar every init bLock.
public void initMapBlocks(int n, int m)
{
this.Dispatcher.Invoke(delegate ()
{
Loader.Visibility = Visibility.Visible;
mainWindow.Visibility = Visibility.Hidden;
});
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
this.Dispatcher.Invoke(delegate ()
{
MapPart mp = new MapPart();
mainCanvas.Children.Add(mp);
Loader.addPoint(1);
});
}
}
this.Dispatcher.Invoke(delegate ()
{
Loader.Visibility = Visibility.Hidden;
mainWindow.Visibility = Visibility.Visible;
});
}
Did it`s possible to get full animation?
-------- Update --------
Here code where i add to progress bar value.
public void addPoint(int x)
{
itemCountAlready += x;
loaderBar.Value += x;
}
----- Update 2 -------
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Thread thread = new Thread(new ThreadStart(() => initMapBlocks(100, 100)));
thread.Start();
}
Your initMapBlocks method produces (MapPart) and consumes (adds loader's value and mainCanvas children) at the same time. You can separate by using System.Collections.Generic.Queue
You can define your queue like this
private Queue<MapPart> myQueue = new Queue<MapPart>();
Instead of these guys
mainCanvas.Children.Add(mp);
Loader.addPoint(1);
You can write simply this for produce
myQueue.Enqueue(mp);
And you can define a DispatcherTimer in your constructor for consume
DispatcherTimer myTimer = new DispatcherTimer {
Interval = TimeSpan.FromMilliseconds(1)
};
myTimer.Tick += Consume;
myTimer.Start();
Your Consume method
private void Consume(object sender, EventArgs args) {
if (myQueue.Count > 0) {
//you don't need `this.Dispatcher.Invoke(delegate...` too
loaderBar.Value += myQueue.Dequeue();
mainCanvas.Children.Add(mp);
}
}

How to delete objects so that they no longer use processor power

Basically, i have an endless number of blocks (each built from class "Enemy") being create, stored into a list, and sent animated across the screen. It does this forever. I want to delete the first block after 100 blocks have been created so as not to use too much processing power. Any ideas?
THIS IS THE CODE FOR THE WHOLE CLASS FOR WHICH THE OBJECTS I WANT TO DELETE:
namespace MovementTestV1
{
class Enemy
{
protected Dispatcher dispatcher;
protected Canvas Background;
protected Label Display;
Int32 waitTime;
double EnemyWidth = 53;
Image EnemyImage;
String FilePathImage;
BitmapImage bitPic;
protected double x, y;
System.Windows.Forms.Timer tmr;
double incrementSize = 5.0;
private int i = 0;
public Enemy(Canvas Background, Dispatcher dispatcher, Dictionary<String, String> keys,Label Display, Int32 waitTime = 100)
{
this.Background = Background;
this.dispatcher = dispatcher;
this.waitTime = 70;
//this.keys = keys;
this.Display = Display;
EnemyImage = new Image();
EnemyImage.Width = EnemyWidth;
FilePathImage = #"RedSqare.png";
bitPic = LoadBitmap(FilePathImage, EnemyWidth);
//tmr = new System.Windows.Forms.Timer();
//tmr.Interval = this.waitTime;
//tmr.Tick += new EventHandler(Position);
//tmr.Start();
}
protected BitmapImage LoadBitmap(String assetsRelativePath, double decodeWidth)
{
BitmapImage theBitmap = new BitmapImage();
theBitmap.BeginInit();
String basePath = System.IO.Path.Combine(Environment.CurrentDirectory, #"assets\");
String path = System.IO.Path.Combine(basePath, assetsRelativePath);
theBitmap.UriSource = new Uri(path, UriKind.Absolute);
theBitmap.DecodePixelWidth = (int)decodeWidth;
theBitmap.EndInit();
return theBitmap;
}
public void Place(double x, double y)
{
EnemyImage.Source = bitPic;
this.x = x;
this.y = y;
Background.Children.Add(EnemyImage);
EnemyImage.SetValue(Canvas.LeftProperty, x);
EnemyImage.SetValue(Canvas.TopProperty, y);
tmr = new System.Windows.Forms.Timer();
tmr.Interval = 10;
tmr.Tick += new EventHandler(Position);
tmr.Start();
}
public void Position(object sender, System.EventArgs e)
{
i++;
if (i < 9000)
{
x -= incrementSize *.3;
}
UpdatePosition();
}
void UpdatePosition()
{
EnemyImage.SetValue(Canvas.LeftProperty, x);
EnemyImage.SetValue(Canvas.TopProperty, y);
}
public double X
{
get
{
return x;
}
set
{
x = value;
}
}
public double Y
{
get
{
return y;
}
set
{
y = value;
}
}
public void Shutdown()
{
tmr.Stop();
}
}
}
public void spawn(object sender, System.EventArgs e)
{
Int32 place = random.Next(1, 4);
Enemy enemy;
i += 2;
if (i % 46 == 0)
{
Int32 Ycoord = random.Next(0, 700);
switch (place)
{
case 1:
enemy = new Enemy(Background, dispatcher, keys, Display, 10);
enemy.Place(1080, Ycoord);
break;
case 2:
enemy = new Enemy(Background, dispatcher, keys, Display, 10);
enemy.Place(1080, Ycoord);
break;
default:
enemy = new Enemy(Background, dispatcher, keys, Display, 10);
enemy.Place(1080, Ycoord);
break;
}
enemies.Add(enemy);
}
if (enemies.Count > 5)
{
//THIS PART DOESNT WORK!!!!!
enemies.RemoveAt(0);
//enemies[1] = 0;
////enemies[2] = null;
//enemies[2].Shutdown();
////enemies[3] = null;
//enemies[3].Shutdown();
////enemies[4] = null;
//enemies[4].Shutdown();
}
}
Hard to say without any code to work off of... However, you could just check to see if your list has over 100 blocks, then setting your first blocks to null. C# has a garbage collector that will clean up the mess.
Edit: or use the Remove method stated above.
for ( int i = 0; i < enemies.size(); ++i )
{
if ( enemies.Count > 5 )
{
enemies.Remove(i);
}
}

C# Timer thread crash, BeginInvoke

I've an "animateMyWindow" class to change opened window's opacity with Timer.
namespace POCentury
{
class animateMyWindow
{
Timer _timer1 = new Timer();
Window _openedWindow = null;
public void animationTimerStart(object openedWindow)
{
if (openedWindow == null)
{
throw new Exception("Hata");
}
else
{
_openedWindow = (Window)openedWindow;
_timer1.Interval = 1 * 25;
_timer1.Elapsed += new ElapsedEventHandler(animationStart);
_timer1.AutoReset = true;
_timer1.Enabled = true;
_timer1.Start();
}
}
private void animationStart(object sender, ElapsedEventArgs e)
{
if (_openedWindow.Opacity == 1)
animationStop();
else
_openedWindow.Opacity += .1;
}
private void animationStop()
{
_timer1.Stop();
}
}
}
animationStart function can't reach my window because it is working on a different thread.
I've tried Dispatcher.BeginInvoke and can't make it work.
Can you help me with doing that?
Basically, you can't access the openedWindow inside the animationStart event because it's happening in a different thread. You need the Dispatcher to do that.
Dispatcher.BeginInvoke(new Action(() =>
{
if (_openedWindow.Opacity == 1)
animationStop();
else
_openedWindow.Opacity += .1;
}));

How can I alter XAML elements from a backgroundworker thread?

In the following code example, I want to change the color of the Foreground text of a TextBox from my BackgroundThread, but it gets the error:
This thread cannot access this object
since it is in another thread.
What do I have to change in the code below so that the background worker thread can change the foreground color in the TextBox?
Answer:
Thanks Andy, it was just that small oversight, here is the corrected code for posterity's sake:
using System.Windows;
using System.ComponentModel;
using System.Threading;
using System.Windows.Media;
namespace TestBackgroundWorker7338
{
public partial class Window1 : Window
{
private BackgroundWorker _worker;
int _percentageFinished = 0;
public Window1()
{
InitializeComponent();
ButtonCancel.IsEnabled = false;
}
private void Button_Start(object sender, RoutedEventArgs e)
{
_worker = new BackgroundWorker();
_worker.WorkerReportsProgress = true;
_worker.WorkerSupportsCancellation = true;
_worker.DoWork += (s, args) =>
{
BackgroundWorker worker = s as BackgroundWorker;
int numberOfTasks = 300;
for (int i = 1; i <= numberOfTasks; i++)
{
if (worker.CancellationPending)
{
args.Cancel = true;
return;
}
Thread.Sleep(10);
float percentageDone = (i / (float)numberOfTasks) * 100f;
worker.ReportProgress((int)percentageDone);
}
};
_worker.ProgressChanged += (s,args) =>
{
_percentageFinished = args.ProgressPercentage;
ProgressBar.Value = _percentageFinished;
Message.Text = _percentageFinished + "% finished";
if (_percentageFinished < 500)
{
Message.Text = "stopped at " + _percentageFinished + "%";
}
else
{
Message.Text = "finished";
}
if (_percentageFinished >= 70)
{
InputBox.Foreground = new SolidColorBrush(Colors.Red);
}
else if (_percentageFinished >= 40)
{
InputBox.Foreground = new SolidColorBrush(Colors.Orange);
}
else if (_percentageFinished >= 10)
{
InputBox.Foreground = new SolidColorBrush(Colors.Brown);
}
else
{
InputBox.Foreground = new SolidColorBrush(Colors.Black);
}
};
_worker.RunWorkerCompleted += (s,args) =>
{
ButtonStart.IsEnabled = true;
ButtonCancel.IsEnabled = false;
ProgressBar.Value = 0;
};
_worker.RunWorkerAsync();
ButtonStart.IsEnabled = false;
ButtonCancel.IsEnabled = true;
}
private void Button_Cancel(object sender, RoutedEventArgs e)
{
_worker.CancelAsync();
}
}
}
As Andy said changing properties of controls must happen on the UI thread.
WPF provides a Dispatcher class that makes it easier to route calls for controls to the appropriate UI thread. Controls in WPF exposes a Dispatcher property object to dispatch the call to an appropriate UI thread.
You can use this as follows (I would add this after the line worker.ReportProgress((int)percentageDone); in the Button_Start method):
// the Window class exposes a Dispatcher property, alternatively you could use
// InputBox.Dispatcher to the same effect
if (!Dispatcher.CheckAccess())
{
Dispatcher.Invoke(new Action(() => ChangeForegroundColor(percentageDone));
}
else
{
ChangeForegroundColor(percentageDone);
}
...
private void ChangeForegroundColor(int percentageDone)
{
if (percentageDone >= 90)
{
InputBox.Foreground = new SolidColorBrush(Colors.Red);
}
else if(percentageDone >=10)
{
InputBox.Foreground = new SolidColorBrush(Colors.Orange);
}
}
The ProgressChanged delegate is run on the UI thread. If you set the BackgroundColor there instead of in DoWork, that should allow you to set the color without the error.

Categories

Resources