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.