Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Odd, seems to be some sort of bug in .NET (C#?) with marshalling the argument to the worker thread.</p> <p>If you implement IConvertable on the passed struct:</p> <pre><code>struct MyPair&lt;TKey, TValue&gt; : IConvertable { public readonly TKey Key; public readonly TValue Value; public MyPair(TKey key, TValue value) { Key = key; Value = value; } // I just used the smart-tag on IConvertable to get all these... // public X ToX(IFormatProvider provider) { throw new InvalidCastException(); } ... public object ToType(Type conversionType, IFormatProvider provider) { if (typeof(MyPair&lt;TKey, TValue&gt;).GUID == conversionType.GUID) return this; throw new InvalidCastException(); } } </code></pre> <p>It runs fine. The passed conversionType doesnt pass .Equal(), IsAssignableFrom(), or anything else I tried except GUID comparison, which is probably related to why it asks for an IConvertable in the first place.</p> <p>EDIT: A simple workaround is to use closures to pass the parameter:</p> <pre><code>var data = new Dictionary&lt;string, string&gt; { { "Hello", "World" }, { "How are", "You?" }, { "Goodbye", "World!" } }; foreach (var pair in data) { var copy = pair; // define a different variable for each worker Action worker = () =&gt; Console.WriteLine("Item {0}, {1}", copy.Key, copy.Value); worker.BeginInvoke(null, null); } </code></pre> <p>Of course, if you need the results, you will need to store the IAsyncResults, which will probably have the same issue as parameters, in the other direction. As an alternative, you could add them to a collection when they are complete, but the locking gets a bit weird:</p> <pre><code>var data = new Dictionary&lt;string, string&gt; { { "Hello", "World" }, { "How are", "You?" }, { "Goodbye", "World!" } }; var results = new List&lt;KeyValuePair&lt;string, string&gt;&gt;(); var pending = 0; var done = new ManualResetEvent(false); var workers = new List&lt;Action&gt;(); foreach (var pair in data) { ++pending; var copy = pair; // define a different variable for each worker workers.Add(delegate() { Console.WriteLine("Item {0}, {1}", copy.Key, copy.Value); lock (results) results.Add(new KeyValuePair&lt;string, string&gt;("New " + copy.Key, "New " + copy.Value)); if (0 == Interlocked.Decrement(ref pending)) done.Set(); }); } foreach (var worker in workers) worker.BeginInvoke(null, null); done.WaitOne(); foreach (var pair in results) Console.WriteLine("Result {0}, {1}", pair.Key, pair.Value); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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