Blog Archives

WCF Service Library in Windows Service

References
CodeProject (WCF Service in Windows Service), http://www.codeproject.com/KB/WCF/WCF_windows_hosting.aspx
MSDN (Debug Windows Service Applications), http://msdn.microsoft.com/en-us/library/7a50syb3(v=vs.80).aspx
MSDN (Host WCF Service in Managed Windows Service), http://msdn.microsoft.com/en-us/library/ms733069.aspx

C# Application Not Responding Issue on Long Running Threads

Undoubtedly many of you have encountered the dreaded “Application Not Responding” message, programmers and non-programmers alike.

In general, this is caused by the OS misinterpretation of long delays in form refresh, even though these can easily be caused by a long running process, or any running process longer than a couple seconds actually.

If this is happening in your application after clicking on a button or event fires, a quick solution would be the easy one-liner below:

Application.DoEvents();

However, to really understand why this happening, remember that all processes (may) contain multiple threads.

In the case of your typical quick shot solution that you put together in VS in no-time, you’re likely only using a single thread.

This thread is used not only to run the processes in your application, but also to update the form window including drawing the fields and various controls on the form.

The best solution to this would really be to create a separate thread and pass off the work of your long running function to this thread.

When the work is finished, it will update our main thread (the form thread) that it has completed which we can intercept with a delegate, and notify the user of success or failure.

See snippet below.

private BackgroundWorker hardworker; //this guys works hard!
private System.Timers.Timer lazyworker; //notice how this could also be achieved with thread/timer

protected void btnDoAlotOfWork_Click() {
//lets see who accomplishes more!
ClockInLazyWorker();
ClockInHardWorker();
}

private void ClockInLazyWorker() {
lazyworker = new System.Timers.Timer(); //not to be confused with forms timer
lazyworker.Interval = 1000; //time in milliseconds. 10 seconds is lazy for a computer!
            lazyworker.Elapsed += new System.Timers.ElapsedEventHandler(LetsGetAMoveOnIt());
}

private void ClockInHardWorker() {
hardworker= new BackgroundWorker();
            mCopier.DoWork += DoWork; //all the heavy lifting is done here
            mCopier.RunWorkerCompleted += WorkCompleted();
            mCopier.WorkerSupportsCancellation = true;
            OnError += ProblemWithWork();
}

//motivational function name made especially for our lazy worker
private void LetsGetAMoveOnIt() {
//do something that takes a long time
//if the data you operate on is the same as used by another thread or the form thread, make sure you wrap this block in a synclock or use a bool to track when work is in process
}

//created separate function for the background worker, and also to avoid synclock issues since there are two new active threads
private void DoWork() {
//if the data you operate on is the same as used by another thread or the form thread, make sure you wrap this block in a synclock or use a bool to track when work is in process. same goes for timer.
}

private void WorkCompleted() {
//all finished
}

private void ProblemWithWork() {
//hopefully not an injury on the job! that's workmans comp!
}

Alot more code than simply “Application.DoEvents”.. but do not underestimate the power of multiple threads!

Burn Hybrid CDs using Free Tools

CD Burning in general is supported in most operating systems right out of the box.

However, if you’re looking for a free alternative to features which are otherwise considered “premium”, like burning hybrid CD’s that work on Windows and Mac there are freely available tools licensed under GNU which have everything you need.

Consider the steps below. Simple (if you follow the steps and don’t have hiccups), free (as long as GNU stands) and error proof (so long as you use the precompiled binaries of stable versions and don’t stray to bleeding edge BETAs or compiling your own :P).

Step 1

For Windows:
Download and install Cygwin.
During install process, towards the end where you select packages, select cdrtools/cdrecord/mkisofs

For Mac:
Download cdrtools from their website or freshmeat.

For (Redhat) Linux:
su
yum install cdrtools OR yum install cdrecord THEN
yum install mkisofs

For (Debian) Linux:
su
apt-get install cdrtools OR apt-get install cdrecord THEN
apt-get install mkisofs

Step 2

Prep a folder that will be burned to the disc. Make sure the contents will fit and verify the file structure is correct.

If you want to literally burn the folder itself, don’t “cd” into that directory. If you are burning the contents of the folder, you will “cd” into the directory and specify “.” as the target name instead of the folder name.

Step 3

In Windows start up Cygwin, on Mac open the darwin terminal window, on linux open your terminal window.

Run:
cdrecord -scanbus

Note the numerical identifier for your burner drive. This is what you will specify for the “dev” parameter later.

“cd” into the directory containing the files to burn then run:
mkisofs -o isoname.iso -r -J -hfs .

Then from within the same directory, burn the iso that you created:
cdrecord dev=4,0,0 -v isoname.iso

That’s it! Let ‘er rip! ;)

Note: genisoimage is similar to mkisofs, but I have not used it extensively and cannot verify if functionality is exactly the same.

References
Cygwin, http://www.cygwin.com/
CDRTools, http://cdrecord.berlios.de
Freshmeat, http://freshmeat.net
Howto-Pages.org, http://howto-pages.org/cdwriting/07.php
G-Loaded.eu, http://www.g-loaded.eu/2007/04/25/how-to-create-a-windows-bootable-cd-with-mkisofs/
Brandonhutchinson.com, http://www.brandonhutchinson.com/Burning_CDs_with_cdrecord.html
genisoimage, http://linux.die.net/man/1/genisoimage
Hints.Macword, http://hints.macworld.com/article.php?story=20020520083853834

Access Denied on File Delete or Cannot delete file

If you receive an “access denied” message on your PC, whether in Windows or Linux, which occurs when trying to delete or modify a file, it can usually be narrowed down to a few reasons:

- The file is marked as readonly
- The file is located on a CD, disk, drive, removable device or any other storage type which is either marked as readonly for the entire volume or your files are contained within a readonly partition in this volume (which can easily occur in linux if you manually mount a drive improperly)
- You do not have access to delete the file or are not an owner of the file
- The file is currently open or in use

If you are deleting the file using code in your .Net application, be sure to remove the “read only” attribute which won’t prevent a user from deleting it, but will definitely halt your application. (code reference).

If you are absolutely sure you should be able to delete the file, or are trying to remove a file which be malicious or difficult to delete, there are tools available such as file assassin which can be used to override the normal OS safeguards (if you have the right know how and permissions) and delete the file anyway.

References
Imran Akram (WordPress BLOG)http://imak47.wordpress.com/2009/01/15/how-to-remove-readonly-attribute-from-a-file/
MalwareBytes “FileAssassin”, http://www.malwarebytes.org/fileassassin.php

Bind Control to an Object in Windows Forms

Simple solution for basic binding in Windows Forms app. This would NOT be recommended if you are using ASP .Net, Silverlight, WCF, RIA or any other services to retrieve the data as these project types have much better support for binding to controls.

C#:

static class dataaccess {
static mytype object;
}

//on app load
object = new mytype();

//on form load
tbField1.DataBindings.Add("Text", dataaccess.object.property, "Field1")
Follow

Get every new post delivered to your Inbox.