Access the control from a thread that does not create a control

Source: Internet
Author: User

Class testThread

 

Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace testThread
{
Public class MessageSendEventArgs: EventArgs
{
Public String Message;
Public MessageSendEventArgs (string message)
{
This. Message = message;
}
}
Class SubThread
{
Public delegate void MessageSendEventHandler (object sender, MessageSendEventArgs e );
// Define the event
Public event MessageSendEventHandler MessageSend;
Protected virtual void OnMessageSend (MessageSendEventArgs e)
{
If (MessageSend! = Null)
MessageSend (this, e );
}
// Define a thread
Public System. Threading. Thread sendthread;
Public void Sendding ()
{
While (true)
{
System. Threading. Thread. Sleep (500 );
This. OnMessageSend (new MessageSendEventArgs (DateTime. Now. ToString ()));
}
}
// Start the thread function
Public void StartSend ()
{
Sendthread = new System. Threading. Thread (new System. Threading. ThreadStart (Sendding ));
Sendthread. Start ();
}
// End the thread function
Public void EndSend ()
{
Sendthread. Abort ();
}
}
}

 

In the main form, use Control. checkforillegalcrossthreadcils = false;

You can return to. net1.1 to modify the control from a non-main thread by default. You can also use the invoke method.

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;

Namespace testThread
{
Public partial class Form1: Form
{

Public delegate void MessageHandler (MessageSendEventArgs e );
SubThread _ subthread; // define a thread

Public Form1 ()
{
InitializeComponent ();
// Control. checkforillegalcrossthreadcils = false;
This. _ subthread = new SubThread (); // create a thread instance
This. _ subthread. MessageSend + = this. _ subthread_MessageSend;
// This. _ subthread. MessageSend + = this. _ subthread_MessageSend; // subscribe to events
}
Public void MessageAdd (MessageSendEventArgs e)
{
ListBox1.Items. Add (e. Message );
}

Private void _ subthread_MessageSend (object sender, MessageSendEventArgs e)
{
// Instantiate the proxy
MessageHandler handler = new MessageHandler (MessageAdd );
/// Call Invoke
This. Invoke (handler, new object [] {e });
// ListBox1.Items. Add (e. Message );
}

Private void button#click (object sender, EventArgs e)
{
This. _ subthread. StartSend ();
This. listBox1.Items. Add ("start ....");
}

Private void button2_Click (object sender, EventArgs e)
{
This. _ subthread. EndSend ();
This. listBox1.Items. Add ("stop ");
}

}
}

 

 

 

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.