English 中文(简体)
什么最容易“在一个单独的read子里办案”?
原标题:What is the easiest way to "run an instance in a separate Thread"?

我试图通过将每个过程(等级)放在一个单独的路口来模拟分配的算法,以便它们成为真正的孤立过程。 这些进程应能相互沟通。

我试图做些什么,可以通过这一法典来证明:

public class Process
{
    public void Run()
    {
        Console.WriteLine("Run called from thread {0}", Thread.CurrentThread.ManagedThreadId);
    }

    public void Fnc()
    {
        Console.WriteLine("Fnc called from thread {0}", Thread.CurrentThread.ManagedThreadId);
        Thread.Sleep(1000);
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine("Main is running in thread {0}", Thread.CurrentThread.ManagedThreadId);

        Process p1 = new Process();

        var t1 = new Thread(p1.Run);
        t1.Start();


        // This should call Fnc() in t1 Thread. It should also return immediatelly not waiting for method Fnc() to finish.
        p1.Fnc();

        Console.ReadLine();
    }
}

I am getting this output:

Main is running in thread 9
Run called from thread 10
Fnc called from thread 9

我想得到这样的东西:

Main is running in thread 9
Run called from thread 10
Fnc called from thread 10

能否实现这种功能?

Thank you!

最佳回答

• 图书馆:

System.Threading.Tasks.Task.Factory.StartNew( ( ) => p1.Run )
    .ContinueWith( ( t ) => p1.Fnc );

或者,你创立了一个小型助手方法:

class Program
{
    private static Process p1 = new Process();
    static void Main()
    {
        Console.WriteLine("Main is running in thread {0}", Thread.CurrentThread.ManagedThreadId);

        var t1 = new Thread(Helper);
        t1.Start();
        Console.ReadLine();
    }

    private static Helper( )
    {
        p.Run();
        p.Fnc();
    }
}
问题回答

@PVitt说,它没有可靠地测试你gon子的交付。 您所需要的是real单独程序。

创建可执行且具有不同指挥线参数和(或)使用任何可动用的皇家警察部队。 NET 相互建立网络框架。

没有任何机制可以选择一种具体途径来操作一种方法,除非这种透镜明确是为了支持这种方法。 这种透镜的基本成分是接收包件的快乐和read。 否则,作为producer/consumer problem。 你们要求的是简单地实施:

public void Run()
{
    Fnc();
}
Action action = () => { p.Run(); p.Fnc(); };
var t1 = new Thread(action);
t1.Start();

如果你使用的话。 净额4,I d建议使用任务(http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx),这确实容易管理/处理。

处理你刚才需要写的话:

Task.Factory.StartNew(()=>{p.Run(); p.Fnc();});




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签