通过引用传递struct实例到ParameterizedThreadStart

时间:2013-03-30 10:22:11

标签: c# struct pass-by-reference

我想启动一个线程,并作为参数通过引用传递一个struct的实例。 ref关键字在使用线程之前工作正常但我现在似乎无法使用ref

这就是我目前的代码:

Thread melodyThread = new Thread(new ParameterizedThreadStart(PlayMelody));
melodyThread.Start(melody1);

private void PlayMelody(object parameter)
{
    Melody melody = (Melody)parameter;
    .
    .
    .
}

我想通过引用传递struct Melodymelody1)的实例。感谢。

1 个答案:

答案 0 :(得分:1)

我可以想到几种可能性。

解决方案一:使用包装类

using System;
using System.Threading;

namespace Demo
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var melody = new Melody { Value = 1 };
            var wrapper = new MelodyWrapper { Melody = melody };

            Thread melodyThread = new Thread(() => PlayMelody(wrapper));

            melodyThread.Start();
            melodyThread.Join();

            Console.WriteLine(wrapper.Melody.Value);
        }

        private static void PlayMelody(MelodyWrapper wrapper)
        {
            Console.WriteLine(wrapper.Melody.Value);
            Thread.Sleep(1000);
            wrapper.Melody.Value = 2;
        }
    }

    public struct Melody
    {
        public int Value;
    }

    public class MelodyWrapper
    {
        public Melody Melody;
    }
}

或者,不使用Lambda:

using System;
using System.Threading;

namespace Demo
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var melody = new Melody { Value = 1 };
            var wrapper = new MelodyWrapper { Melody = melody };

            Thread melodyThread = new Thread(PlayMelody);

            melodyThread.Start(wrapper);
            melodyThread.Join();

            Console.WriteLine(wrapper.Melody.Value);
        }

        private static void PlayMelody(object parameter)
        {
            MelodyWrapper wrapper = (MelodyWrapper)parameter;
            Console.WriteLine(wrapper.Melody.Value);
            Thread.Sleep(1000);
            wrapper.Melody.Value = 2;
        }
    }

    public struct Melody
    {
        public int Value;
    }

    public class MelodyWrapper
    {
        public Melody Melody;
    }
}

解决方案二:使用代理并返回Melody的新值

using System;
using System.Threading;

namespace Demo
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var melody = new Melody { Value = 1 };
            Func<Melody, Melody> play = PlayMelody;
            var result = play.BeginInvoke(melody, null, null);
            melody = play.EndInvoke(result);
            Console.WriteLine(melody.Value);
        }

        private static Melody PlayMelody(Melody melody)
        {
            Console.WriteLine(melody.Value);
            Thread.Sleep(1000);
            melody.Value = 2;
            return melody;
        }
    }

    public struct Melody
    {
        public int Value;
    }
}

我个人赞成第二种解决方案。因为它返回一个新值,所以如果您使用此解决方案,则可以使Melody不可变。

(我的首选解决方案使用Task<Melody>,但您无法使用任务。)