English 中文(简体)
C# - 努力利用作为行动参数的职能建立已翻新的进程
原标题:C# - Trying to create a threaded process using a function that has as Action as a parameter

这个项目是Im为我自己做的事情。

我开始尝试结合和相互交错。 在假释申请中,我有以下法典:

    public static void Save(string newWord)
    {
        using (var db = new MyDataContext())
        {
            var w = new Word {word = newWord};
            db.Words.InsertOnSubmit(w);
            db.SubmitChanges();
        }
    }

    static void Main(string[] args)
    {
        var letters = new[] {  A ,  B ,  C ,  1 ,  2 ,  3 };

        for (var i = 2; i < 10; i++)
        {
            letters.GetPermutations(a => Save(string.Join(string.Empty, a.ToArray())), i, true);
        }
    }

在延长期间,我有制定合并法。 我在此为那些希望审查的人找到了综合守则(http://blog.noldorin.com/ 201005/combinatorics-in-csharp/)。

    public static void GetCombinations<T>(this IList<T> list, Action<IList<T>> action, int? resultSize = null, bool withRepetition = false)
    {
        if (list == null)
            throw new ArgumentNullException("list");
        if (action == null)
            throw new ArgumentNullException("action");
        if (resultSize.HasValue && resultSize.Value <= 0)
            throw new ArgumentException(errorMessageValueLessThanZero, "resultSize");

        var result = new T[resultSize.HasValue ? resultSize.Value : list.Count];
        var indices = new int[result.Length];

        for (int i = 0; i < indices.Length; i++)
            indices[i] = withRepetition ? -1 : indices.Length - i - 2;

        int curIndex = 0;

        while (curIndex != -1)
        {
            indices[curIndex]++;

            if (indices[curIndex] == (curIndex == 0 ? list.Count : indices[curIndex - 1] + (withRepetition ? 1 : 0)))
            {
                indices[curIndex] = withRepetition ? -1 : indices.Length - curIndex - 2;
                curIndex--;
            }
            else
            {
                result[curIndex] = list[indices[curIndex]];
                if (curIndex < indices.Length - 1)
                    curIndex++;
                else
                    action(result);
            }
        }
    }

然后,我认为,在清单中计算所有守护者的组合是冷静的,每份都放在自己的表面。 因此,在我的旁边,我尝试了。

Thread t = new Thread(letters.GetPermutations(a => Save(string.Join(string.Empty, a.ToArray())), i, true));

但显然,正在通过的《行动》,即呼吁“救助”职能,并不像现在这样。 如果有人能给我一个正确方向的判断,我不胜感激。

Thanks, Andy

最佳回答

<代码>Thread的构造者正在寻找一名代表,但你似乎转折。 填入“<代码> 代表。

ThreadStart del = () => letters.GetPermutations(a => Save(string.Join(string.Empty, a.ToArray())), i, true);
Thread t = new Thread(del);
t.Start();
问题回答

暂无回答




相关问题
Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Is reading from an XmlDocument object thread safe?

I was wondering if i could safely read from an XmlDocument object using SelectNodes() and SelectSingleNode() from multiple threads with no problems. MSDN says that they are not guaranteed to be ...

Terminating a thread gracefully not using TerminateThread()

My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function. At the moment I am using ...

热门标签