Home > Net >  C # to monitor events (parameters)
C # to monitor events (parameters)

Time:10-10

I want to ask about a problem in c # to monitor events, for example, suppose I want to listen a read data function, when the data is read to 100, and then to pass the data of the 100 read to another data processing function to deal with, and then continue to read data on my side, could you tell me the parameters passed directly or need to be transmitted by another way, because I am a beginner, is still on the stage of learning, ask everybody to give directions, thank you

CodePudding user response:

The building Lord you can know about the delegate

CodePudding user response:

reference 1st floor it_gz_xi response:
do you think you could understand the original poster to entrust

You can write a general framework, because I will only write with no parameters to entrust

CodePudding user response:

 delegate void Evt (byte [] data); 
Evt DataRead=new Evt (DataRead=& gt;
{
//On the Data Read
});
Public void ReadData ()
{
Var dataread=new byte [100];
DataRead? Invoke (dataread);
}

CodePudding user response:

https://blog.csdn.net/dqs78833488/article/details/51331341

CodePudding user response:

Can be written in various ways, help you wrote a simple:
Interface is as follows:

CodePudding user response:

 public partial class Form1: Form 
{
Private Random r=new Random ();
Private static object obj=new object ();
Private bool CanExit=false;
Private DataProcess P=new DataProcess (a);

Public _click ()
{
InitializeComponent ();
}

Private void button1_Click (object sender, EventArgs e)
{
Thread the Thread=new Thread (ListDataRead);
Thread. IsBackground=true;
Thread. The Start ();

//the event was to receive data to simulate other data processing program
Conviction yourself ataReadCompleteHandlerEvent +=new Action (DataReceive_Complete);
}

Private void DataReceive_Complete (List The list)
{
If (a list!=null)
{
AppendText (" received new data number "+ list. Count. The ToString ());
for (int i=0; I & lt; List. The Count; I++)
{
AppendText (the list [I] the ToString ());
}
}
}

Private void AppendText (string strInfo)
{
If (textBox1. InvokeRequired)
{
Var _handler=new Action (AppendText);
TextBox1. The BeginInvoke (_handler strInfo);
}
The else
{
TextBox1. AppendText (strInfo + Environment. NewLine);
}
}

Private void ListDataRead ()
{
The Monitor. The Enter (obj);

List List=new List (a);
while (! CanExit)
{
Int data=(https://bbs.csdn.net/topics/ReadData);
List. The Add (data);

If (list. Count & gt;
=100){
P.P rocess100Data (list);
List. The Clear ();
}

Thread.sleep (10);
}

The Monitor. The Exit (obj);
}

///& lt; Summary>
///analog reading data function
///& lt;/summary>
Private int ReadData ()
{
The int data=https://bbs.csdn.net/topics/r.Next (0100);
Return the data.
}
}

CodePudding user response:

Special data processing categories:

using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. The Text;

The namespace WindowsFormsApplication8
{
Public class DataProcess
{
Public Action DataReadCompleteHandlerEvent;

Public void Process100Data data (T)
{
If (DataReadCompleteHandlerEvent!=null)
{
DataReadCompleteHandlerEvent. Invoke (data);
}
}
}
}

CodePudding user response:

refer to 7th floor god brother reply:
special data processing categories:

using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. The Text;

The namespace WindowsFormsApplication8
{
Public class DataProcess
{
Public Action DataReadCompleteHandlerEvent;

Public void Process100Data data (T)
{
If (DataReadCompleteHandlerEvent!=null)
{
DataReadCompleteHandlerEvent. Invoke (data);
}
}
}
}

Thank I saw your case, I seemed to understand a lot

CodePudding user response:

The UI thread to prevent blocking, so want to use, or asynchronous multi-threaded, or it will stuck interface, complete until the logic
  •  Tags:  
  • C#
  • Related