Multi-thread writing

Source: Internet
Author: User

Int testfunc (string Str)
{
MessageBox. Show (STR );
Return 1;
}

 

Void test ()
{
This. lblmessage. Text = "111 ";
}

Private void form1_load (Object sender, eventargs E)
{
// Threadpool. queueuserworkitem (New waitcallback (x =>
//{
// Func <string, int> func = new func <string, int> (this. testfunc );
// This. Invoke (func, "Fuck ");
// This. Invoke (new action (this. Test ));
// Thread. Sleep (5000 );
// MessageBox. Show ("YYYY ");
//}));

// MessageBox. Show ("XXXX ");

// Dowitheasy ();
// Dowithparameter ();
// Dowithtimer ();
// Dowiththreadpool ();
// Dowiththreadpoolparameter ();
// Dowithanonymous ();
// Dowithlambda ();
// Dowithcommon ();
// Dowithaction ();
// Dowithfunc ();
// Dowithpredicate ();
}

# Region a simple thread
Private void dowitheasy ()
{
Thread t = new thread (New threadstart (this. dosomethingwitheasy ));
T. Start ();
}
Private void dosomethingwitheasy ()
{
MessageBox. Show ("Knights warrior ");
}
# Endregion

# Region a simple thread with Parameter
Private void dowithparameter ()
{
Thread t = new thread (New parameterizedthreadstart (this. dosomethingwithparameter ));
T. Start ("Knights warrior ");
}
Private void dosomethingwithparameter (Object X)
{
MessageBox. Show (X. tostring ());
}
# Endregion

# Region thread with Timer
Public void dowithtimer ()
{

System. Windows. Forms. Timer timer = new system. Windows. Forms. Timer ();
Timer. interval = 1000;
Timer. Tick + = (x, y) =>
{
MessageBox. Show ("Knights warrior ");
};
Timer. Start ();
}
# Endregion

# Region threadpool with no Parameter
Private void dowiththreadpool ()
{
Threadpool. queueuserworkitem (New waitcallback (this. dosomethingwiththreadpoolno ));
}
Private void dosomethingwiththreadpoolno (Object X)
{
MessageBox. Show ("Knights warrior ");
}
# Endregion

# Region threadpool with Parameter
Private void dowiththreadpoolparameter ()
{
Threadpool. queueuserworkitem (New waitcallback (this. dosomethingwiththreadpoolparameter), "Knights warrior ");
}
Private void dosomethingwiththreadpoolparameter (Object X)
{
MessageBox. Show (X. tostring ());
}
# Endregion

# Region threadpool with anonymous delegate
Private void dowithanonymous ()
{
Threadpool. queueuserworkitem (New waitcallback (delegate (Object X)
{
MessageBox. Show ("Knights warrior ");
}));
}
# Endregion

# Region threadpool with Lambda
Private void dowithlambda ()
{
Threadpool. queueuserworkitem (New waitcallback (x =>
{
MessageBox. Show ("Knights warrior ");
}));
}
# Endregion

# Region thread change UI
Private void dowithcommon ()
{
Waitcallback = new waitcallback (this. invokemethod );
Threadpool. queueuserworkitem (waitcallback, "Knights warrior ");
}

Private delegate void invokemethoddelegate (string name );
Private void invokemethod (Object X)
{
This. Invoke (New invokemethoddelegate (this. changeuiwithcommon), X. tostring ());
}

Private void changeuiwithcommon (string name)
{
This. lblmessage. Text = Name;
}
# Endregion

# Region thread change UI with action
Private void dowithaction ()
{
Waitcallback = new waitcallback (this. dosomethingwithaction );
Threadpool. queueuserworkitem (waitcallback, "Knights warrior ");
}

Private void dosomethingwithaction (Object X)
{
This. Invoke (new action <string> (this. changeui), X. tostring ());
}

Private void changeui (string message)
{
This. lblmessage. Text = message;
}
# Endregion

# Region thread change UI with func
Private void dowithfunc ()
{
Waitcallback = new waitcallback (this. dosomethingwithfunc );
Threadpool. queueuserworkitem (waitcallback, "Knights warrior ");
}

Private void dosomethingwithfunc (Object X)
{
Func <string, int> F = new func <string, int> (this. getfuncmessage );
Object result = This. Invoke (f, x. tostring ());
MessageBox. Show (result. tostring ());
}

Private int getfuncmessage (string message)
{
This. lblmessage. Text = message;
If (Message = "Knights warrior ")
{
Return 1;
}
Else
{
Return 0;
}
}
# Endregion

# Region thread change UI with Predicate
Private void dowithpredicate ()
{
Waitcallback = new waitcallback (this. dosomethingwithpredicate );
Threadpool. queueuserworkitem (waitcallback, "Knights warrior ");
}

Private void dosomethingwithpredicate (Object X)
{
Predicate <string> Pd = new predicate <string> (this. getpredicatemessage );
Object result = This. Invoke (PD, X );
MessageBox. Show (result. tostring ());
}

Private bool getpredicatemessage (string message)
{
This. lblmessage. Text = message;
If (Message = "Knights warrior ")
{
Return true;
}
Else
{
Return false;
}
}
# Endregion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.