Simple multi-threading issue - c#

I have a class called Movement which moves a picture box (guy on the left) on my form to a random position when it's Lead method gets executed. I also have a method which moves another picture box (dark guy on the right) called SetMrShadowToMove. These two picture boxes are suppose to move at the same time and arrive at the random point (one point for both picture boxes) at the same time.
For now, these methods use a timer to move the picture boxes, but my main intention was to have a loop which runs until the picture box's coordinates meet the random point picked while using Thread.Sleep to delay the process and make an animation of it.
To do that, I need to run both methods on different threads, so my UI thread doesn't get frozen and these methods would run at the same time.
My problem is that I don't know how to run these methods on different threads, and also don't know how to access the picture boxes on the other threads (it seems that this has something to do with invoking and stuff!).
What is the best way to solve this problem?

The best way (in my opinion) is by using a background worker.
those things can do there calculations in the background. If you you want to update the interface after that you can use there report progress event to invoke your main (interface) thread and redraw the screen. You can also the work complete even if you want to have a event of the background worker as a final even (for example if mister shadow is 'destroyed' or something).
I don't know your code of mister shadow so I don't have any direct example code for you. But a full example about how to use a background worker can be found on msdn here. I hope this will help you.

Related

DataVisualization.Charting.Chart PostPaint Event Firing Prematurely

I have a complex chart that I'm drawing and the PostPaint event for System.Windows.Forms.DataVisualization.Charting.Chart seems to be firing prematurely. There is no chart on the screen yet.
Does anyone have a better understanding of the behavior of the PostPaint event with large charts? Is there a better event to listen to? Recommended strategies?
I'm not sure posting code would provide any benefit.
ORIGINAL POST:
So it turns out the answer to this (for me anyway), is to create another thread (I'm doing it with a Task) off of the event. This thread's job is to continually compare the region with an empty image and when it is no longer empty, grab the image and save it in the database. Because it's a thread, this frees up the chart to eventually render itself.
FINAL POST:
In the end, I had to put a lock inside the PostPaint event (it was firing multiple times as TaW indicated it would) and make sure the code inside would only execute a single time (I have a Draw button so these controls get reset every time it's clicked). Additionally, it was necessary for me to create a task to actually do the work of capturing the image and storing it in the database. If I didn't do this, I would get a blank image every time. I did not actually have to check point count or sample the image to see if it was blank.
Locking down the event and creating the task inside the event is what made the difference.

Animated Busy Indicator in Compact Framework 2.0

So I'm pulling some stuff from a database via a different thread, and want the form to remain usable while doing so, and also want to show the user a progress indicator until the DB code finishes.
In 'normal' winforms I'd use a ProgressBar with the ProgressBarStyle set to Continuous, but CF doesn't have that. Neither does it seem to support animated GIFs in a PictureBox (which would have been an easy way).
So what are my options for doing something like this?
EDIT: Don't want to use an hourglass mouse cursor because that implies to the user that the UI is busy (it's not).
Thanks
You could look for a 3rd party progress bar control that supports the Continuous progress bar style, or a control that supports animated gifs - you're not the first running into this limitation, but I'm not sure if there are any good ones out there.
Rolling your own "continuous" progress bar UserControl shouldn't be very hard, you get a decent result with just a timer to call Invalidate, and using Graphics.FillRectangle in the Paint event.
An alternative to an animated gif control could be to create a "film strip" UserControl, where you provide an image (non-gif) that contains all "frames" of the gif layed out horizontally or vertically. Again you'd need a timer to call Invalidate and increase the frame number, and Graphics.DrawImage has an overload to specify which portion of the film strip image is drawn.
Instead of the timer you could use #josef's comment to increment the "current frame" whenever the worker thread has finished a portion of the work. The animated gif's "movement" would then actually show the user that work is being done.

Unity progress bar on button click to load another scene

I want to create a button which loads the scene as well as shows the loading progress bar.
How to do this?
Thank You !
You need to know how to use Co routine for this to work. Because you will use LoadLevelAsync, which will run your Co routine call together with the LoadLevelAsync. So while loading, your call for the Progress bar will check the current progress and have a boolean check if the Load is complete.
Using the new UI scroll bar for loading is easier but it I would need to in form you the step by step to set it up, cause it is mainly an editor set up rather than code. So I will redirect you to the old GUI function which takes care of it. This has been answered in Unity3d Answer page already.
http://answers.unity3d.com/questions/457594/how-to-make-a-progress-bar-for-loading-next-scene.html
You can use above mention method or below simple method
You can use Scrollbar of Unity UI and one background texture.
Just make value =0 and Size =0.5 and load your scene , and when scene is loaded make size 1 and disable Scrollbar and background texture. :-)

How to create a visualization for a timer job

In my SL application I have a DispatchTimer which periodically refreshes data on the screen. I would like to create a visualisation for the user to show when the next refresh will happen.
My first thought was to change the timer (which currently runs every 20 seconds) to run every millisecond and, each tick, update a ProgressBar and count-down till the 20 seconds are up to run the main update method.
But I cannot shake the feeling that there's probably a better way of doing this. This feels quite heavy.
IS there a better way of doing this?
I would make an animated progress bar which resets on reaching the end.
Easiest would be to create two rectangles, one on top of antother with different colors.
Animate width of the one on the top from 0 to length of your progress bar.
You can control it by making a property which indicates how long should the animation last.
And maybe some start method to synchronize with refresh actions.
EDIT. There is example on msdn HERE (examples section)
The idea of a progres bar is not that bad. Updating every milisecond or each tick is much to often. 10 times a second would be enough.

Animate a e.Graphics.DrawLine with arrowhead

I need to "animate" an arrow. It can go in any direction (up, down, left, right and diagonals) and the arrowhead will be at the end. It needs to appear as if it is growing from orgin to the end.
To make matters more complicated it has to be in a user control so it is possible to add to the controls collection in the forms and remove it to delete it from the screen, and its background is really transparent (no color.transparent) so i can't paint the background to "clear" the previous line.
I have it static already (drawn from origin to end) but i can't animate it. I tried to add a timer in the user control but I fail to get a correct algorithm to calculate the intermediate ends of the line while it is growing.
Any help would be appreciated.
Thanks
When using animation on a Windows form you have to use another thread. Look into using a background worker: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
You don't need another thread - I have succesfully implemented Windows Forms animation.
My design:
Use a Windows.System.Forms.Timer object to call a callback function 'Animate()' at regular intervals
the Animate() function updates a property of your arrow, and then calls Invalidate() on the windows control
this all happens in the same UI thread, so yuo will not get any flicker effects (as long as your control has double duffering switched on).

Categories

Resources