标题:c#中一个知识点的问题!困惑中!
只看楼主
zmidl
Rank: 1
等 级:新手上路
帖 子:115
专家分:0
注 册:2009-4-3
结帖率:90.32%
已结贴  问题点数:20 回复次数:2 
c#中一个知识点的问题!困惑中!
开始学习接触多线程编程 下载了论坛里一个摇奖实例进行学习。其中一段代码很困惑,希望得到指点
代码如下:
using System;
using System.Windows.Forms;
using System.Threading;
  

namespace TransBall
{
    /// <summary>
    /// Class1 オトユェメェヒオテ?」
    /// </summary>
    public class InThread
    {
        private Control A;
        private int Seed;

        public void RunInThread()
        {
            Random N = new Random(Seed);
            do
            {
                A.Text = N.Next(0,10).ToString();
                Thread.Sleep(10);
            }
            while(true);
        }

        public void SetNumber(Control B, int SeedHere)
        {
                           A = B;
            Seed= SeedHere;
        }
    }
}
困惑的是, SetNumber 方法起到什么作用?为什么声明Control B 和 int SeedHere 两个参数 然后把 A附值B Seed附值SeedHere?为什么SetNumber里面没有具体的方法函数呢? A = B;Seed= SeedHere;有什么意义和作用呢??
搜索更多相关主题的帖子: 多线程 
2009-08-10 10:31
jedypjd
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:9
帖 子:1096
专家分:4969
注 册:2009-7-27
得分:14 
贴上源码,这样才能帮你

天涯无岁月,歧路有风尘,百年浑似醉,是非一片云
2009-08-10 10:37
zmidl
Rank: 1
等 级:新手上路
帖 子:115
专家分:0
注 册:2009-4-3
得分:0 
using System;
using System.Drawing;
using System.Collections;
using
using System.Windows.Forms;
using System.Data;
using System.Runtime;  
using System.Threading;  
//using System.Diagnostics;
  
namespace TransBall
{
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class MainForm : System.Windows.Forms.Form
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        ///  
        private Point _mousePosition;
        private Point _formPosition;
        private bool isMouseDown = false;
        private bool IfThreadStart;
        Thread T1;
        Thread T2;
        Thread T3;
        Thread T4;
        Thread T5;
        Thread T6;
 
        private delegate void CALL(Control Tr);
 
        private System.Windows.Forms.PictureBox btnExit;
        private System.Windows.Forms.PictureBox btnStart;
        private System.Windows.Forms.Label L1;
        private System.Windows.Forms.Label L2;
        private System.Windows.Forms.Label L3;
        private System.Windows.Forms.Label L4;
        private System.Windows.Forms.Label L5;
        private System.Windows.Forms.Label L6;
        private System.Windows.Forms.Label lblLast;
        private components = null;
 
        public MainForm()
        {
            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();
 
            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //
        }
 
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)  
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
         
        [STAThread]
        static void Main()  
        {
            Application.Run(new MainForm());
        }
 
        private void MainForm_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)  
            {
                _mousePosition = new Point( e.X, e.Y);//基于窗体的坐标
                _formPosition = this.Location;
                isMouseDown = true;
            }
 
        }
 
        private void MainForm_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (isMouseDown)  
            {
            
                Point currentMousePosition = Control.MousePosition;//光标相对于屏幕左上角的坐标
                Point location = new Point(
                    (currentMousePosition.X - _mousePosition.X),
                    (currentMousePosition.Y - _mousePosition.Y));
  
                this.Location = location;
//            
//                Debug.WriteLine( "this.Location.X:" + this.Location.X.ToString() );
//                Debug.WriteLine( "Control.MousePosition.X:" + Control.MousePosition.X.ToString() );
//                Debug.WriteLine( "Location.X:" + location.X.ToString() );
            }
        }
 
        private void MainForm_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)  
            {
                isMouseDown = false;
            }
        }
 
        private void btnExit_Click(object sender, System.EventArgs e)
        {
            this.Close();
            Application.Exit();  
        }
 
        private void btnStart_Click(object sender, System.EventArgs e)
        {
            if (IfThreadStart==false)
            {
                lblLast.Visible=false;
                InThread A1 = new InThread();
                A1.SetNumber(L1,000000);
                T1 = new Thread(new ThreadStart(A1.RunInThread));
                T1.Start();
 
                InThread A2 = new InThread();
                A2.SetNumber(L2,111111);
                T2 = new Thread(new ThreadStart(A2.RunInThread));
                T2.Start();  
         
                InThread A3 = new InThread();
                A3.SetNumber(L3,222222);
                T3 = new Thread(new ThreadStart(A3.RunInThread));
                T3.Start();  
 
                InThread A4 = new InThread();
                A4.SetNumber(L4,333333);
                T4 = new Thread(new ThreadStart(A4.RunInThread));
                T4.Start();  
 
                InThread A5 = new InThread();
                A5.SetNumber(L5,444444);
                T5 = new Thread(new ThreadStart(A5.RunInThread));
                T5.Start();  
 
                InThread A6 = new InThread();
                A6.SetNumber(L6,555555);
                T6 = new Thread(new ThreadStart(A6.RunInThread));
                T6.Start();  
 
                IfThreadStart=true;
            }
            else
            {
                T1.Abort();  
                T2.Abort();  
                T3.Abort();  
                T4.Abort();  
                T5.Abort();  
                T6.Abort();  
                lblLast.Text ="中奖号码:"+L1.Text +L2.Text+L3.Text+L4.Text+L5.Text +L6.Text;     
                lblLast.Visible=true;  
                IfThreadStart=false;
            }
        }
 
        private void MainForm_Load(object sender, System.EventArgs e)
        {
            IfThreadStart = false;
        }
    }
}
2009-08-10 13:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-282160-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.134386 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved