How to upload parameters to methods in the delegate method in a thread

Source: Internet
Author: User
Recently, when optimizing my previous projects, I need to pass an object to the methods in the original delegate method, and I want to use this object to dynamically generate a DataTable. An error is reported during running: "The control created on a thread cannot be the parent level of the Control created on another thread ".

So I declare a delegate again, and then I use multithreading again in the method, start another method in this thread to complete the work I want, and then delegate it to another method for implementation.

I am dizzy... Let's talk about it in code. 1 public delegate void data_1Callback (DataTable dataTable); // delegate the DATATABLE operation
2
3Remote. RemoteServer. UserCome + = new Remote. UserComeHandler (RemoteServer_UserCome );
4. This is another delegate of the method in the thread at startup.
5
6 public void RemoteServer_UserCome (object o, string user, string userIP)
7 {stopThread (); // stop the thread beforehand
8 t = new Thread (new ThreadStart (BindDataGrid ));
9 t. Start ();
10
11}
12
13 void stopThread ()
14 {
15 if (t! = Null) & (t. IsAlive ))
16 {
17 t. Abort ();
18 t. Join ();
19}
20 t = null;
21}
22
23 public void BindDataGrid ()
24 {
25 if (dtUser. Rows. Count <1)
26 {
27 ColId. DataType = System. Type. GetType ("System. Int32 ");
28 dtUser. Columns. Add ("ID ");
29 ColDateTime. DataType = System. Type. GetType ("System. String ");
30 dtUser. Columns. Add ("launch time ");
31 dtUser. Columns. Add ("User Name ");
32 dtUser. Columns. Add ("current state ");
33 dtUser. Columns. Add ("Exit Time ");
34 dtUser. Columns. Add ("Internet IP ");
35 dtUser. Columns. Add ("intranet IP ");
36}
37 count ++;
38 DataRow dr = dtUser. NewRow ();
39
40 dr [0] = count;
41 dr [1] = DateTime. Now. ToString ();
42 dr [2] = userName;
43 dr [3] = "online ";
44 dr [4] = "";
45 dr [5] = NetIp;
46 dr [6] = WokIp;
47
48 dtUser. Rows. Add (dr );
49
50dgUser. Invoke (new data_1Callback (data_1), new object [] {dtUser });
51}
52
53 private void data_1 (DataTable dataTable)
54 {
55 try
56 {
57 this. dgUser. DataSource = dataTable;
58 this. dgUser. Refresh ();
59}
60 catch (Exception ex)
61 {
62 MessageBox. Show (ex. Message. ToString ());
63}
64}

It seems a little messy. Leave me a message if you don't understand it.

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.