Thread.Sleep (.NET) vs Sleep Function (WinAPI)


Thread.Sleep is a sign of a poorly designed program.

by Peter Ritchie.

Thread.Sleep has it’s use: simulating lengthy operations while testing/debugging on an MTA thread.  In .NET there’s no other reason to use it.

Thread.Sleep(n) means block the current thread for at least the number of timeslices (or thread quantums) that can occur within n milliseconds.  The length of a timeslice is different on different versions/types of Windows and different processors and generally ranges from 15 to 30 milliseconds.  This means the thread is almost guaranteed to block for more than n milliseconds.  The likelihood that your thread will re-awaken exactly after n milliseconds is about as impossible as impossible can be.  So, Thread.Sleep is pointless for timing.  Read the rest of this entry »

Recursive File Search in C | Source Code


This source code below is written by se7en from LeetCoders.  It is capable of running through the enter C drive in 8 seconds on my computer finding more than 230,000 files.  Although the downside of it is that it costs quite an amount of CPU usage during its process.  You might try to optimize it by placing Sleep function or something that is possible in reducing the CPU usage. Read the rest of this entry »

Auto Updater


I came across a question today and i find it very interesting.  I never thought of this back then when i haven’t tested C’s .dll and VB .NET together.  This post would more be of a theory and it’s pretty simple.  By the way, “Auto Updater” do mean that the program is capable of updating the version itself.

Howto

If you already know how to create a dynamic link library file, i think it’s pretty easy for you to do it.  There is alot of method to do so but what i am suggesting here is to use 2 files.  1 would be your .exe file and the other would be your .dll file.  I’ll assume that you’re building everything in C/++ because i am.

In your main program, you’ll have to decide when you’re going to check for updates and you’ll have to provide the if-else yourself.  I’m only guiding on how will you perform the update for the function.

Now, the idea is to build your functions in a .dll form and your .exe file would execute them.  So whenever a bug occurs it would occur in the .dll file.  And when that happens you could send a trigger to your .exe file maybe by having them check your website whether there is a need for it to do so.  When it does need to update, it performs the update function and it will download automatically from your website (probably a link that you’ve provided) and replace it with the .dll that you previously have.  Voilà!

How do you call a function in a .dll in C/++

Let’s assume the .dll file is called “math.dll” and the function is called “Addition” as below

__declspec(dllexport) short Addition(short num1, short num2)

In order to call the function from the library, we have to first specify the function in our .exe.  The codes below shows the full syntax for what is required.

typedef short (__stdcall *fAddition)(short num1, short num2);
HINSTANCE math = LoadLibrary("math.dll");

fAddition ptAddition = (fAddition)GetProcAddress(math, "Addition");

printf("%d", ptAddition(1, 2));

FreeLibrary(math);

As you can see,
line 1 is used to define the function and its parameters
line 2 is used to load the library into an HINSTANCE variable so that we could free it later
line 4 is used to get the address of the function from the library by using GetProcAddress
line 6 as you can see is to show you that it works
line 8 is used to free the library once we’re done using it

Other information on C/++ calling dynamic link library – http://goffconcepts.com/techarticles/development/cpp/calldll.html

Alternatives

If you think that this is pretty silly for a small program (or some other reason), you might just want to build it in a single executable file.  Have it check when to update and when it does, download the updated executable (the new version) from your website or somewhere and replace it with the original file.  Produce creative ideas and you’ll be able to solve it in your own way.