在线程之间来回传递数据 [英] passing data to and from threads

查看:90
本文介绍了在线程之间来回传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个在MCTS 70-536考试中给出的程序....但无法理解其工作原理...所以我需要支持来学习此程序...

I have created a program which is given in MCTS 70-536 exam....but not able to understand its working...So i need support to learn this...

using System;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Multiply m = new Multiply("Hello, world!", 13, new ResultDelegate(ResultCallback));

            Thread t = new Thread(new ThreadStart(m.ThreadProc));
            t.Start();
            Console.WriteLine("Main thread does some work, then waits.");
            t.Join();
            Console.WriteLine("Thread completed.");

            Console.ReadLine();
        }
        public static void ResultCallback(int retValue)
        {
            Console.WriteLine("Returned value: {0}", retValue);
        }
    }

    public delegate void ResultDelegate(int value);

    public class Multiply
    {
        private string greeting;
        private int value;

        private ResultDelegate callback;

        public Multiply(string _greeting, int _value, ResultDelegate _callback)
        {
            greeting = _greeting;
            value = _value;
            callback = _callback;
        }

        public void ThreadProc()
        {
            Console.WriteLine(greeting);
            if (callback != null)
            {
                callback(value * 2);
            }
        }
    }
}

推荐答案

这说明了线程如何设置可以在线程外使用的值(特别是对检查结果的有用)一个线程,如上面的示例).

如果您无法分析现有代码,那么您作为程序员的职业将会很短.
It''s illustratinbg how a thread can set a value that can be used outside of the thread (especially useful for checking the results of a thread, as in the example above).

If you can''t analyze existing code, your career as a programmer will be short.


Multiply m = new Multiply("Hello, world!", 13, new ResultDelegate(ResultCallback));


该声明中紧随其后的事情
一个.值传递给Multiply类,该类用作Thread的输入.
b.创建了新的Delegate,它将从线程中调用ResultCallback,而后者又将值返回给Program类.

这是该程序的主要本质


Follwing things are happing in this statement
a. Value is passed to the Multiply class which is used as a input to the Thread.
b. New Delegate is created which will call ResultCallback from the Thread, which in turn is returning value back to the Program class.

This is the main essence of this program


这篇关于在线程之间来回传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆