Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert C# function to VB.Net, Lambda problems and best way to convert
    text
    copied!<p>I am trying to convert a block of code from VS2010 C# 4.0 framework to VS2008 VB.NET 3.5 Framework. I am extremely unsure of how to proceed as I am unfamiliar with VB.NET and VS2008 and I am finding the differences between the two environments frustrating. I've gutted most of the irrelvant code from the original C# in hopes of providing a clean example of the problem that a solution can be indicated from.</p> <p>I did try a few code converters but the lambdas tripped them up.</p> <p>I converted the first lambda to a simple <code>For Each</code> construct which seems fine. However the second lambda (passed to to the <code>ThreadPool.QueueUserWorkItem</code> call) is causing me uncertainty. As the lambda is using the <code>taskcount</code> var which is declared outside the lambda body, I am hesitant on the best approach to to make this code friendly to my target environment (VS2008/VB.Net/3.5) and keep the code as similar as possible to the original implementation. I don't wish to explicitly push the <code>taskcount</code> into the <code>WaitCallback</code> unless there is no other way.</p> <pre><code>public void sendAndBlock(List&lt;ISendable&gt; msgs) { int taskCount = msgs.Count; AutoResetEvent fini = new AutoResetEvent(false); msgs.ForEach(m =&gt; { ThreadPool.QueueUserWorkItem(nil =&gt; { this.send(m); //threadsafe decrement and if we have no tasks left then unblock. if (0 == Interlocked.Decrement(ref taskCount)) { fini.Set();//signal our main thread to proceed } }, m); }); //block our main thread fini.WaitOne(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload