Japo's code
Most of the programs I make at work are not suitable or interesting to post here, but I have also made some reusable libraries, and some curious side projects.
I'll start with one of the latter, made in Maple. It calculates the equations of the position of the Sun, relative to any point on the Earth, as a function of the time of the year. Code:
# speed of Earth's orbit in rad/h http://i102.photobucket.com/albums/m...ath/th_sol.png (click) The purpose is to know the position of the sun at any hour of any day of the year, but I think the most visual result is had by plotting the Sun's trajectory along a whole year. Code:
plots[spacecurve]( r, t=0..365.25*24, numpoints=87660, axes=normal, labels=["S-N", "E-W", "z-n"], tickmarks=[0, 0, 0], shading=zhue, thickness=2, orientation=[130, 60]); http://i102.photobucket.com/albums/m.../th_Pucela.png (click) One axis goes from South to North, another from East to West, and the other is the verticality. The colours colder than blueish green are below the horizon--it's night. Another example: the Equator, you can see why there's no Summer or Winter over there: http://i102.photobucket.com/albums/m...th_Equator.png (click) And Murmansk, Russia, above the Arctic Circle. Notice how the Sun doesn't set during some Summer days, and it doesn't dawn during some Winter ones. http://i102.photobucket.com/albums/m...h_Murmansk.png (click) |
xThreading 1
Something more useful today. My preferred programming framework is .NET, and my preferred language C#. With .NET it's very easy to take do multi-threading, so you can perform tasks in parallel, taking advantage of modern CPUs; and most important, so your user interface doesn't stop responding whenever you have to perform a task that takes some time, instead you can perform it in the background while the UI thread goes on in parallel.
Even though the .NET class library does most of the work, I made a little library to take care of some common tasks when multi-threading and cross-threading, I called it "xThreading". First it has a simple extension method to make thread-safe calls to Controls (actually any ISynchronizeInvoke) as detailed here. The arguments it takes are a delegate to the method that you want to run in the Control's thread, and the parameters, if any. (I provided an overload for the frequent case when the method has no parameters nor return value--is a System.Action delegate--because just "Delegate" isn't strongly typed (System.Delegate is abstract) and you'd need an instantiated delegate every time you used this, but the compiler can't infer it because Delegate is abstract. You could make additional overloads for other recurrent situations.) Code:
using System; Code:
frm.xInvoke( () => frm.Close() ); Code:
frm.xInvoke( () => |
xThreading 2
So what I wanted at this point, were classes that presented the same properties and methods as the different Controls. I want them to reference cross-thread Controls, and when anyone calls their members as if they were the Controls themselves, under the hood they'll be using xInvoke() to make the calls in a thread-safe way.
For example, having created one of these objects referencing our Form, Code:
xfrm = new xForm(frm); Quote:
Of course I haven't implemented all the functionality of Windows Forms in the corresponding xControls; actually I have defined very little classes with very little members, whatever I have needed myself so far. For example: Code:
using System.Windows.Forms; Here goes xISynchronizeInvoke<T>, be warned, lots of reflection going on: Code:
using System; |
xThreading 3
And then I wanted another thing--actually this was the first one I wanted to make, and what got me into authoring the rest, that this is built onto. I wanted a splash window to show while a parallel task is carried out in the background, but I wanted the background task to be able to show its status in the splash screen.
Here it is. The constructor takes a delegate to the action you want to perform in parallel, and just creating the splash window starts the task immediately. You won't even need to keep a reference to the window most times, as it closes itself down when the task is done; so even this minimal statement works: Code:
new xSplash(method); The class doesn't provide for returning values from the Task (an System.Action<> returns void), passing parameters to it, or cancelling it from the main thread, simply because I haven't needed it; although it wouldn't be hard to add that. By the way, if a parallel thread throws an exception, it doesn't reach the main thread immediately. But with xSplash, every time you click on it, it will check the task for exceptions, and if there's any it will be thrown back into the main thread; if there's none, nothing will happen. (I guess if the user sees that the task is taking too long and the splash window remains there forever without reporting any change of status, he will instinctively click on it.) Here's the code: Code:
using System.Windows.Forms; There's actually more code of course (InitializeComponent() etc.), what was written automatically with the Visual Studio designer, including non default values for some properties, to make the splash window look the way I wanted--even though it's really nothing fancy and has no graphics etc. I used dynamic layout (everyone should), so the label and the window resize automatically according to the text, depending of the system font size etc. Also you shouldn't let the user close the window, in principle, otherwise when the task is finished and it tries to close it again, you'll get an exception--and without the splash you won't have any way to retrieve it. |
The current time is 01:18 PM (GMT) |
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.