Sunday 15 May 2011

winforms - c# Static or Non Static Class -



winforms - c# Static or Non Static Class -

i have c# windows forms mp3 player application. have sound files in resources folder , separate "myaudio" static class handles sound related work playing , increasing volume etc. form, phone call play method using:

myaudio.play(track);

in myaudio class, have windowsmediaplayer object declared as:

private static windowsmediaplayer obj=new windowsmediaplayer();

my question is, in terms of efficiency , less memory usage, improve declare myaudio class static or non static? wise create object of myaudio class in form , phone call methods or straight phone call using class name? practice declare instance variables static?

your question indeed broad, there few design principles can take care of, while designing class:

do need object , it's state throughout application lifetime do need maintain state of class variables future use do need multi-thread or parallelize application @ point of time do need decouple component in future , used in other scenarios ajax based web scenario

important thing in case keen maintain state application lifetime , amount of memory usage fine application environment, since after initializing able info memory , don't need query source database. however, scenario need initialize 1 time , read static info in rest of application. in case plan re query information, part purpose of using static type lost

let's assume in future need parallelize code performance enhancement, static come haunt you, since shared among threads , invariably need synchronization build lock, mutex, serialize threads , purpose lost. same things happen in web / ajax scenario , static component cannot handle multiple parallel requests , corrupted until , unless synchronized. here instance variable per thread boon, task / info parallelization without requiring lock, mutex

in understanding static convenience, many programmers misuse, avoiding instance variable , using @ will, without understanding implications. gc perspective, cannot collect static variable, working set of application invariably increment till stabilize , not decrease until , unless explicitly released program, not application, until , unless storing info avoid network database calls.

ideal design suggest utilize instance class, gets created, work , gets released, not linger around. in case there's info needs passed 1 function in case play pause stop, info can persisted static variable , modified in thread safe manner, much improve approach

if take illustration given since it's windows form, operations play, static fine, executable running on system, testing imagine scenario initiate multiple instances double clicking , play around on each one, pressing different operations, access same static object , may corruption issue, in fact resolve such scenario may chose class singleton, @ given moment no more 1 instance can exist in memory, happens yahoo messenger, no matter how many times click, same instance comes up.

c# winforms audio static non-static

No comments:

Post a Comment