views:

25

answers:

0
using System;
using System.Text;
using System.Threading;

namespace Functionality_Threading
{
    class Program
    {
        static void Main(string[] args)
        {
            TestA testFunction = new TestA();
            testFunction.EventHandle += new TestA.EventHandlerMain(testFunction_EventHandle);
            var newThread = new Thread(testFunction.TestFunction);
            newThread.Start();
            Console.ReadLine();
        }

        static EventArgs testFunction_EventHandle(object I)
        {
            Console.WriteLine(I + " - " + Thread.CurrentThread.ManagedThreadId);
            return null;
        }

    }

    class TestA
    {
        public delegate EventArgs EventHandlerMain(object I);
        public event EventHandlerMain EventHandle;
        public Thread primary { get; set; }
        public Thread second { get; set; }
        public void TestFunction()
        {
            second = Thread.CurrentThread;
            long Data = 0;
            while (true)
            {
                if (Data + 1 == long.MaxValue)
                    Data = 0;
                else
                    Data++;
                DoSomething(Data);
            }
        }

        public TestA()
        {
            primary = Thread.CurrentThread;
        }

        public void DoSomething(long Data)
        {
            EventHandle.Invoke(Data + " - " + primary.ManagedThreadId + ", " + second.ManagedThreadId +" | "+ Thread.CurrentThread.ManagedThreadId);
        }
    }
}

I have been trying to find a design that "confine" a custom thread to it own class instead of having custom thread going into the other classes from the event. An output that I'm looking for is: Pretend that primary thread that create the class is 1 and the custom thread made in the thread is 2. The output of the program should be: "######## - 1, 2 | 2 - 1"

~Note~ It have nothing to do with the UI thread (ABSOLUTELY NOTHING) and it is entirely a design that I needs to apply to variety of projects. If SynchronousContext is being used, can you please provide an example or modification to this program to clarify how it could be used? Please and thank you.