something about thread(c#)

        thread is very importment element in our project,when we want to manage two or more things ,we can use thread to manage,one thread manage  one thing,that's we ususlly say Multithreading program.

        now,i just want to say something about how to create a thread and then say something about how to work from a thread which is running to another thread.

        how to create thread.first you should use keyword Thread to declare a variable thread ,and should provide a threadstart function to make thread start.  code:                                                                                                                              

            public void printString()
            {
                Console.WriteLine(s1);
            }
            Thread t = new Thread(printString);
            t.Start();

ok,we have created a thread,and make it start. also we can make thread sleeping and make it suspend and so on.

        how to work from one running thread to another thread.we should use delegate that a special declare in c#.

code:

    delegate void setEndable(bool bs, Form e);

        private void SetEndable(bool bs, Form e)
        {
            if ((this.InvokeRequired) & (!this.IsDisposed))
            {
                setEndable d = new setEndable(SetEndable);
                this.Invoke(d, new object[] { bs, e });
            }
            else
            {
                e.Enabled = true;
            }
        }             

in this code ,we  use delegate void setEndable(bool bs, Form e) to declare a method setEnable,this method have two parament : one is bool variable bs and another is Form variabel e,and  then we make setEnable method have it own body. in it's body,first we should judge this thread wheather has been engrossed,if didn't ,we can immediately do thing just like e.Enabled = true,if has been engrossed,first we should declare a SetEndableobject d , and then use Invokemethod to transfer original method has been declared  as delegate .we shoule notice that parament should keep the same with announce(new object[] { bs, e } like (bool bs, Form e)).actually,that's use a recursive method to provide a safe method.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值