Tuesday, January 26, 2010

Web Part Page Maintenance and some url tricks

Sometime during development a web part might encounter unhandled exceptions and that will cause your page to have the red colored error text to appear.

In some cases you want to remove this problimatic web part, to do this simply append "?contents=1" to the url. The web part maintenance will appear and you can remove the web part with error.

Some other url tricks to manage sharepoint page view:

?PageView=Shared&ToolPaneView=2 --> Change everyone's view of a particular page.
?PageView=Personal&ToolPaneView=2 --> change private view of a particular page.

cheers :).

Monday, January 4, 2010

Thread Creation with ManualResetEvent

Use ManualResetEvent to synchronize the multiple threads that are running. In this example the main thread will wait for all the threads to complete before exiting the caller method by using ManualResetEvent.WaitAll.

In the code below, I created a pattern with thread creation. Remeber that creating a thread is quite expensive, so remember tha you shoul create a thread only when you know how many process you will be running simutaneously and it may take time to process meaning it may run for 10 seconds or more.

If you have more than 20 threads to be created then try to execute them in batch specially if these threads will take time to process. Let 20 threads to finish first before creating another new batch of threads. This pattern was demostrated in the code below, with some code comments.

But if you have multiple processes that may take only for a few seconds then maybe consider using ThreadPool, which I will be discussing in my next blog.

Code:



Output:



Note:

You will encounter an error when using ManualResetEvent.WaitAll with a single threaded method meaning when a method has a declared [STAThread]

See a good pattern on how to use ManualResetEvent with a Single Threaded Method.

Threading with anonymous method

One of the enhancements to C# in version 2.0 is anonymous methods. These allow you to specify blocks of code as methods within other methods, and use those methods as delegates. You can access variables (including local variables and parameters of the "outside" method) within the anonymous method.

So with this enhancement it is now very easy to pass delegated method in a created thread.

Before you need to pass an object and cast it inside the delegated function.

for example, if you need to pass multiple strings to a delegated function, then you need to use delimeter and then parse it in delegated function, as shown in the example below.



Now with the enhancement with .Net 2.0, it easier.

Using with ThreadPool

ThreadPool.QueueUserWorkItem(delegate { d.Worker2("my string parameter"); });

or with lambda exporession

ThreadPool.QueueUserWorkItem(e => d.Worker2("my string parameter"));

Using with ParameterizedThreadStart

Thread t = new Thread(new ParameterizedThreadStart(delegate{ d.Worker("my string parameter");}));

Code Example: