|
You can format currency like this:
decimal value = -19230023.33M;
string format = string.Format("{0:C}", value);
|
|
|
|
|
You could create an extension method that returns the value formatted as a currency.
string currency = value.FormattedCurrency();
"There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C.A.R. Hoare
Home | LinkedIn | Google+ | Twitter
|
|
|
|
|
I could, but then again, I'm not the one asking the question
|
|
|
|
|
Oops let me re-post this to the OP
"There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C.A.R. Hoare
Home | LinkedIn | Google+ | Twitter
|
|
|
|
|
I HIGHLY suggest you read up on string.Format() . It'll make your life sooooo much easier than trying to figure out which quote marks pair up with which.
return string.Format("{0} ({1:N0} at {2:C2} each)", Product.Name, Quantity, Product.UnitPrice);
|
|
|
|
|
Exactly. And it is an important step for globalization / localization, as the order of words may be different in other languages.
|
|
|
|
|
You could create an extension method that returns the value formatted as a currency.
string currency = value.FormattedCurrency();
"There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C.A.R. Hoare
Home | LinkedIn | Google+ | Twitter
|
|
|
|
|
Hi there.
The .NET applications I provide for our test team are using the same set of assemblys. Depending on which test teams uses the app the corresponing assembly will be loaded at runtime.
Easy stuff. One must just handle the AppDomain.ResolveAssembly event properly and everything is fine. So when the app isn't able to resolve a type I simple check all loaded assemblys.
Doing that i came up with the idea, that if I cannot find a depended assembly it is properly because it is missing in the apps working directory. So I could copy it from our file server to the WD and simple load it.
With this mechanism I am able to roll out a new app simple by distributing the executable and on start the app will collect it's dependencies by itself.
Neat stuff. Worked fine for anything invovling WinddowsForms and WPF.
Now with console application the hole thing went kaputt.
Instead of any event being triggered the hole console application just crashed. NO UnhandledException even or any catch just a big kernel32 APPCRASH.
After a hole day of brainfuck, I found a solution but I don't get it.
Here is the thing:
Normally console applications aren't that big so the mayor coding stays in the main(string[] args) method.
Here is a constructed example:
public static void Main(string [ ] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler ( ConsoleExceptionHandler.CurrentDomain_UnhandledExceptionConsole);
AppDomain.CurrentDomain.AssemblyResolve += (AssemblyResolver.CurrentDomain_AssemblyResolve);
Console.WriteLine ( typeof ( JobData ).ToString () );
Console.WriteLine ( typeof ( TSExecTask ).ToString () );
Console.WriteLine ( typeof ( ReportingTask).ToString () );
Console.ReadLine ();
}
which does not work. But if I move the three Console.WriteLine in a separate class (ConsoleCrap) it works.
public static void Main(string [ ] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler ( ConsoleExceptionHandler.CurrentDomain_UnhandledExceptionConsole);
AppDomain.CurrentDomain.AssemblyResolve += (AssemblyResolver.CurrentDomain_AssemblyResolve);
System.Threading.Thread.Sleep ( 100 );
new ConsoleCrap ().WriteSome ();
Console.ReadLine ();
}
I thought maybe, by postponing the point where the missing types are needed, the event has the nessesary time to collect the assemblys.
But the AsssemblyResolve event isn't asynchronous. So the actual execution flow is on hold while the type is not resolved.
On the other hand maybe the issue is, that by using the missing types directly the class containing the the static main() method cannot be laoded as it is missing the assembly before the AssemblyResolve event is registered.
So my question to all knowing more of coding then myself. (basically the rest of the universe)
Maybe someone si abel to givve ma some insight?
|
|
|
|
|
Console-applications come from the era of single-threading. There is no message-pump, so yes, it will be blocking. And yes, any types referenced in a method are loaded before the method is executed.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I once had a very similar problem. I was using something like this.
Thread worker = null;
try
{
new Thread(new ThreadStart(threadFunc));
}
catch (Exception)
{
}
.
.
.
void threadFunc()
{
try
{
}
catch (Exception)
{
}
}
The damn thing kept crashing on me in spite of the try/catch. I finally found out, that an assembly was missing that was referenced in threadFunc. So the Framework tried to load the assembly when inspecting the function, and that was already in the new thread (so the try in the main thread didn't work) but outside of my code.
The solution was to use an intermediate function (with a try/catch inside) similar to the way you are doing it with your ConsoleCrap class. So the lesson learned was: Don't put anything that can crash on load in a function that is directly called by the framework.
The good thing about pessimism is, that you are always either right or pleasently surprised.
|
|
|
|
|
Hi,
I need to improve performance of wpf application. Application is taking too much time to load (almost 15 sec).
As per current behavior application is loading a lot of data from service (kinda lookup Data). Different function are implemented for getting various data from service and already System.Threading.Tasks.Parallel is used to invoke to load data.
Can you please help where I should look to improve performance of application during loading.
PFB the implemented code on client side
SplashWindow.OpenSplash();
System.Threading.Tasks.Parallel.Invoke(
() =>
{
//Load the user preferneces
SplashWindow.SetProgress("Load Lookup Tables...");
LookupTableBL.Instance.Load();
},
() =>
{
LookupTableBL.Instance.LoadHighPriority();
}
);
System.Threading.Tasks.Parallel.Invoke(
() =>
{
//Load the user preferneces
SplashWindow.SetProgress("Load User Preferences...");
UserPreferences.Instance.Load();
},
() =>
{
//start log user entry
SplashWindow.SetProgress("Log User Entry...");
UserEntries.Instance.StartUserEntry();
}
);
SplashWindow.SetProgress("Initialize Component...");
InitializeComponent();
|
|
|
|
|
Yeah, 15 secs to open an app is WAY too long. Windows itself can boot up faster then that. No offense, but I'd just uninstall an app that took 15 seconds to open and use a different one. That being said, you didn't show what any of those .Load() methods are doing or what the .StartUserEntry() method is doing. So I can't really give you any advice beyond one or more of those methods are slow. You need to find out which one and why its slow before you can fix it. Taking a few timings with DateTime.TotalMilliseconds will point you in the right direction.
modified 12-Nov-14 13:16pm.
|
|
|
|
|
load is actually loading a lot of data from hosted service(most of them are dictionaries) e.g.
public void Load(
<pre lang="vb">System.Threading.Tasks.Parallel.Invoke(
() => this.KanbanNoteStateIndicators = LookupTableInnerBL.Instance.GetKanbanNoteStateIndicators()
, () => this.Disciplines = LookupTableInnerBL.Instance.GetDisciplines()
, () => this.MilestoneTypes = LookupTableInnerBL.Instance.GetMilestoneTypes()
, () => this.EpicRequirementTypes = LookupTableInnerBL.Instance.GetEpicRequirementTypes()..... total of 25 to load data which is later used in the applicaiton
}</pre>
Same way we have load resources where are loading resources parallelly from service
private void GetResourcesFromServer()
{
this.Resources = LookupTableInnerBL.Instance.GetResources();
this.ResourcesDictionary = Resources.AsParallel().ToDictionary((item) => item.ID);
}
StartUserEntry is function where current user Identity is logged into database.
|
|
|
|
|
Well, then there you go, that's why your app is slow to start up.
* how many MB of data are you loading combined?
* unless I misread your explanation, you are also making 20 to 30 or more calls into the web service? That's going to be slow.
You need to reconsider your design.
Do you really need to suck down the entire database at start up? Probably not. Yes, it is easier to code it that way, but it will be a performance hit. Especially for customers with slower connections to your web service.
Without understanding your UI or what this data is, its hard to say.
Is your app an internal business app? If so, using a web service as a middle man is overkill and silly. A better design is to use something like EntityFramework which will automatically grab only the data thats needed. If you need to service external customers, then you'll need a web service, of course.
How is your Web Service configured? REST? XML? Soap? If your client is the only client accessing the service, then using a text based web service is silly and lots of additional overhead to all the data. Switch to NetTCPBinding.
Just tossing some ideas out there based on what you posted... but generally, it looks like you are sucking down the entire database (and adding a lot of overhead to it) at start up when thats really not a good idea.
|
|
|
|
|
|
Data is not that big as it is only a kind of look up data (like our standards for the app that never changes). Rest we have whole db from which we are getting data at run time. I checked time of all these calls. There are few methods(2-3) which takes 1-1.5, rest are either 0.001 sec - .4 sec.
Our app is internal business app having around 2000 users and we are already using NetTcp Binding
|
|
|
|
|
Ok, so 3 * 1.5 = 4.5s, 20 * .05 (avg) = 1s. So lets say on average just your start up cost is going to be 4s to 6s.
If you are spending 5s to load app standards and resources that never change, why not just embed them into the app as an XML file or something? Or if you are super paranoid about having to change them some day, implement a caching mechanism. I.e. download the resources and keep a version number. You have version 1.0. Do a single WCF call and see that the current version is 1.1, so you need to re-rab the data. Otherwise use the local copy.
If you have NEVER changed that stuff, its unlikely you ever will, so just embed it into the app.
But you've still only accounted for at most 6s of your start up time. Lets even say your splash screen is putting another 2s on top of that. That's still 8s and you are claiming the start up time is 15s. So where is that other 8s going to?
Usually for something like that, I have a vague idea of where to look, but if you don't, I would use the Visual Studio profiler that's built in. It's good enough for this job.
Also, instead of timing each individual call now, how long does the entire parallel load take?
Also, not sure if you are able to, but if you can, comment out all the load stuff and see how fast your app loads.
If you don't want to hunt around, just use the profiler to see where the cost is.
|
|
|
|
|
Hi Sledge,
I am able to reduce few call which includes an update on splash screen, out of 3 it is not reduced to 2 (saved 1 call).
But still need to reduce call for 1 method
System.Threading.Tasks.Parallel.Invoke(
() =>
{
SplashWindow.SetProgress("Load User Preferences...");
UserPreferences.Instance.Load();
}
;
DB call (file size is 350KB)
SELECT userPreferencesDoc
FROM Application_UserPreferences
WHERE resourceId = 99199
Rest 8 sec is all variable ... when I host service on local it reduces to 1-2 sec but on test environment 6-8 sec
How can I control network issues? any ideas
Also I will verify with profiler and will Update results
|
|
|
|
|
If it's 15s in your prod environment, and 1 - 2s locally, then I'd assume that your prod environment is not up to snuff.
* How many machines are hosting your web service in prod? hardware / memory specs? what kind of network connection? etc. From my experience with WCF, I don't think you'd be able to host 2000 simultaneous connections on one machine. Especially if its an off the shelf PC with limited hardware. You aren't going to be able to use a $2000 Dell PC for this.
* What's your SQL box look like? Is it a seperate box from the web servers? Or is it running on the same box. SQL generally requires VERY beefy hardware in a production environment. I.e. a typical production SQL box at my company is spec'ed out at 24 core Xenon + 140GB RAM + flash storage + 10Gbps backbone and there are still some performance issues.
* If you don't have the resources for a beefy SQL box, the work around for that would be to cache all this static data in the web service as static data upon start up.
What I mean is, right now it sounds like (and corret me if I'm wrong) as the app is making 20 to 30 WCF calls upon start up and those 20 to 30 WCF calls make 20 to 30 SQL calls?
A better design would be to have the WCF service make the 20 to 30 SQL calls ONCE upon start up and keep the data in static members in the exact format you will need to return them. So now all your WCF methods would just be:
return _dictWhatever;
no need to lock or syncronize anything for multi-threading in WCF since it'll be created when the service starts up.
|
|
|
|
|
Hi Sledge,
In my local environment it is taking 5-6 sec but in production 10-15 sec. This 8 sec variable is in all environment (tst,uat,prod) and I don't know how can I control
SQL is already on different VM and service is hosted on a different server.
Caching is already implemented so we we are getting most of the data from cache only.
Service is getting restarted every night and data & cache got refreshed every night. So not every service call is going to SQL call.
Issues that I am facing are
Network issues (prod is too slow than system but if we citrix app it is fast)
There is XML file (350kb) which is imported from database is consuming some time (it is not cached in service)
Also there are few Active Directory calls which is taking (methods that i talked having 0-5 sec) {used for finding groups of user etc.}
Also want to thank you for spending so much time and sharing your knowledge. Kudos !!!
|
|
|
|
|
We tried using VMs here instead of physical boxes. For us, they are too slow and we are a relatively low volume shop. Its noon here and we've only gotten 57,000 requests so far. We are a data intensive shop though, so each request takes like 4 or 5 seconds of database activity (big data).
Have you tried monitoring your web server and sql box in task manager during the day to monitor CPU and memory? If you are maxing out memory and/or CPU, you need to beef up the hardware.
|
|
|
|
|
Loading the splash-screen takes time. You can improve performance by eliminating it. I would also recommend against loading anything before the first UI is displayed. Load the UI first, then kick off a thread to load your data. Load the data using a backgroundworker with NORMAL priority. Any "high priority" thread will slow down the execution of the other running threads - it is recommended to do everything in "normal" priority.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi Eddy,
Loading splash screen is must for us So I can't remove it
Here name of function is loadWithHighPriority is confusing but it is just another parallel task done with normal priority but separately thus named like this.
This data we load is then used even this.loaded to fill data in UI.
|
|
|
|
|
teek13 wrote: Loading splash screen is must for us So I can't remove it You also said it was a must to become faster
I'd recommend to not load anything until the UI is present. A lot can be deferred to a later point in the application, when it is actually used.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
If you want to see where all the time is really being taken up (and Sledge is onto something here), you should run a performance profiler to see what's taking the most time. Don't guess at the problem, get some actual metrics to identify what's going on.
|
|
|
|