Delegate.BeginInvoke vs ThreadPool.QueueUserWorkItem

3 Comments »

ThreadPool is performance-wise faster.

ThreadPool is fire and forget in the sense that BeginInvoke should always be paired with EndInvoke because exceptions are passed to the EndInvoke and should be handled here to avoid the exceptions lingering in memory.

If you don’t care about exceptions and have a relatively simple delegate that should be invoked, use ThreadPool.

If you have more complex delegates where things could go wrong that you care about, use Delegate.BeginInvoke/EndInvoke.

An example of how to use the ThreadPool.QueueUserWorkItem and lambda expressions:


ThreadPool.QueueUserWorkItem(callback => SomeMethod(someArgument));

Alternatively:


WaitCallback waitCallback = new WaitCallback(callback => { x => x*x});

ThreadPool.QueueUserWorkItem(waitCallback);
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Remove .svn or _svn directories batch

No Comments »

The following batch file will search for “_svn” directories, clean the out and remove them.

I put it in a file called cleansvndir.bat and ran “cleansvndir.bat >> result.txt” from the command prompt.

<div id="_mcePaste">@echo off</div>
<div id="_mcePaste">pushd</div>
<div id="_mcePaste">for /f "usebackq tokens=*" %%d in (`"dir *_svn* /ad/b/s | sort /R"`) do (</div>
<div id="_mcePaste">ECHO "cd %%d"</div>
<div id="_mcePaste">pushd "%%d"</div>
<div id="_mcePaste">attrib -r *.* /s</div>
<div id="_mcePaste">ECHO "Deleting all in %%d"</div>
<div id="_mcePaste">del *.* /q</div>
<div id="_mcePaste">for /f "usebackq tokens=*" %%e in (`"dir /ad/b/s"`) do (</div>
<div id="_mcePaste">ECHO "cd %%e"</div>
<div id="_mcePaste">pushd "%%e"</div>
<div id="_mcePaste">ECHO "Deleting all in %%e"</div>
<div id="_mcePaste">del *.* /q</div>
<div id="_mcePaste">popd</div>
<div id="_mcePaste">ECHO "Removing dir %%e"</div>
<div id="_mcePaste">rd "%%e"</div>
<div id="_mcePaste">)</div>
<div id="_mcePaste">popd</div>
<div id="_mcePaste">ECHO "Removing %%d"</div>
<div id="_mcePaste">rd "%%d"</div>
<div id="_mcePaste">)</div>
@echo offpushd
for /f "usebackq tokens=*" %%d in (`"dir *_svn* /ad/b/s | sort /R"`) do (	ECHO "cd %%d"	pushd "%%d"	attrib -r *.* /s
ECHO "Deleting all in %%d"	del *.* /q
for /f "usebackq tokens=*" %%e in (`"dir /ad/b/s"`) do (			ECHO "cd %%e"			pushd "%%e"
ECHO "Deleting all in %%e"			del *.* /q
popd

ECHO "Removing dir %%e"			rd "%%e"		)	popd
ECHO "Removing %%d"	rd "%%d")

Run twice!

Batch info.

This suggests that the long filenames could be wrapped in double quotes (“”%%e”") instead of using the tokens=* parameter.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...