Use msagent in c #

xiaoxiao2021-03-06  40

When I check the information, I suddenly saw Msagent programming, I found some of the information to write a few lines of code.

If there is no Msagent Runtime or SDK, you can download www.microsoft.com. If you use VC , you need to download two header files.

After creating .NET projects, add a reference, select the COM page, and select Microsoft Agent Server 2.0. And quote the namespace of the AgentServerObject where you need to use the Agent.

Using system;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

Using agentServerObjects;

Using system.threading;

Namespace HelloAgent

{

///

/// Form1 summary description.

///

Public Class Form1: System.Windows.Forms.form

{

Private system.windows.Forms.Button Btndelegate;

PRIVATE AGENTSERVER AGENTSERVER = NULL;

Private IAGENTEX AGENTEX = NULL;

Private iAgentCharacterex Characterex = NULL;

Private IAGENTNOTIFYSINK SINK = NULL;

Private Int dwcharid = 0;

Private int dreqid = 0;

Private Int dwsinkid = 0;

Private string strcharname = "c: //windows//msagent//Chars//merlin.acs";

///

/// The required designer variable.

///

Private system.componentmodel.Container Components = NULL;

Public Form1 ()

{

//

// Windows Form Designer Support

//

InitializationComponent ();

//

// Todo: Add any constructor code after INITIALIZECOMPONENT call

//

}

///

/// Clean all the resources being used.

///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code

///

/// Designer supports the required method - do not use the code editor to modify

/// This method is content.

///

Private vidinitiRizeComponent ()

{

This.btndelegate = new system.windows.Forms.Button ();

THIS.SUSPENDLAYOUT ();

//

// btndLegate

//

This.btndelegate.location = new system.drawing.point (96, 32); this.btndelegate.name = "btndelegate";

this.btndelegate.tabindex = 0;

this.btndelegate.text = "delegate";

This.btndelegate.click = new system.eventhandler (this.btndelegate_click);

//

// Form1

//

THIS.AUTOSCALEBASESIZE = New System.drawing.size (6, 14);

THIS.CLIENTSIZE = New System.drawing.size (292, 273);

This.Controls.add (this.btndelegate);

THIS.NAME = "Form1";

THIS.TEXT = "Form1";

This.Load = New System.EventHandler (this.form1_load);

This.ResumeLayout (false);

}

#ndregion

///

/// The main entry point for the application.

///

[Stathread]

Static void

Main

()

{

Application.run (New Form1 ());

}

private vidinit ()

{

AgentServer = new agentServerObjects.AgentServerClass ();

IF (agentserver == null)

{

Messagebox.show ("Agent Initialization Failure");

}

Agentex = (iAgentex) AgentServer;

Agentex.Load (StrcharName, Out Dwcharid, Out dwreqid);

Agentex.getCharacterex (DWcharid, Out Characterex);

Characterex.show (100, Out dwreqid);

Characterex.Speak ("Hello", NULL, OUT DWREQID;

String strname = "";

Characterex.getname (Out Strname);

Characterex.speak ("My Name IS" Strname, Null, Out dwreqid;

Sink = new agent (new speakdlegate (speaksomething);

Agentex.Register (Sink, Out dwsinkid);

}

Private Void Form1_Load (Object Sender, System.EventArgs E)

{

INITAGENT ();

}

Public Delegate Void SpeakDelegate (String Str);

Public void speaksomething (String Str)

{

IF (InvokeRequired)

{

BeginInvoke (New SpeakDelegate (Speaksomething), New Object [] {str};

Return;

}

Characterex.wait (1000, Out dwreqid);

Thread.sleep (1000); Characterex.Speak (Str, Null, Out dwreqid);

}

Private void threadproc ()

{

Speaksomething ("Hello, this is use delegate.");

}

Private void btndelegate_click (Object Sender, System.EventArgs E)

{

Thread thread = New Thread (New ThreadStart (threadproc);

Thread.isbackground = true;

Thread.start ();

}

}

}

The code does not add, because I can't find the place you need to comment, please bear with me.

Agent's event needs to inherit some interfaces under the AgengServerObject name space.

Using system;

Using agentServerObjects;

Namespace HelloAgent

{

///

///

///

Public Class Agentevent: IAGENTNOTIFYSINK

{

Private form1.speakdelegate DELG = NULL;

Public agentevent (form1.speakdelegate DELG)

{

//

// TODO: Add constructor logic here

//

THIS.DELG = DELG;

}

#Region IAGENTNOTIFYSINK member

Public void Command (int dwcommandid, object punkuserinput)

{

// Todo: Add aGENTEVENT.COMMAND implementation

Delg ("Command");

}

Public void DragStart (int dwcharid, short fwkeys, int x, int y)

{

// Todo: Add agentevent.dragStart implementation

Delg ("DragStart");

}

Public void requestStart (int dwrequestid)

{

// Todo: Add aGENTEVENT.REQUESTSTART implementation

// DELG ("Requeststart");

}

Public Void VisibleState (int dwcharid, int bvisible, int dwcause)

{

// Todo: Add aGENTEVENT.VISibleState implementation

Delg ("VisibleState");

}

Public Void BalloonvisibleState (int dwcharid, int bvisible)

{

// Todo: Add Agentevent.balloonvisibleState implementation

// DELG ("BalloonVisibleState);

}

Public void Dragcomplete (int dwcharid, short fwkeys, int x, int y)

{

// Todo: Add aGENTEvent.dragcomplete implementation

Delg ("Dragcomplete");

}

Public Void Click (int dwcharid, short fwkeys, int x, int y)

{

// Todo: Add aGENTEVENT.CLICK implementation

DELG ("Click");

}

Public void dblclick (int dwcharid, short fwkeys, int x, int y) {

// Todo: Add aGENTEVENT.DBLCLICK implementation

Delg ("dblclick");

}

Public void shutdown ()

{

// Todo: Add agentevent.shutdown implementation

Delg ("Shutdown");

}

Public void size (int dwcharid, int lwidth, int Lheight)

{

// Todo: Add aGENTEVENT.SIZE implementation

Delg ("size");

}

Public void restart ()

{

// Todo: Add aGENTEVENT.RESTART implementation

DELG ("Restart");

}

Public Void Bookmark (int dwbookmarkid)

{

// Todo: Add Agentevent.Bookmark implementation

Delg ("Bookmark");

}

Public Void Requestcomplete (int dwrequestid, int hrstatus)

{

// Todo: Add aGENTEVENT.REQUESTCOMPLETE implementation

// DELG ("RequestComplete");

}

Public Void Move (int dwcharid, int x, int y, int dwcause)

{

// Todo: Add aGENTEVENT.MOVE implementation

Delg ("Move");

}

Public void IDle (int dwcharid, int bstart)

{

// Todo: Add aGENTEVENT.IDLE implementation

// DELG ("iDLE");

}

Public Void ActivateInputState (int dwcharid, int bactiVated)

{

// Todo: Add agentevent.ActivateInputState implementation

Delg ("ActivateInputState);

}

#ndregion

}

}

Calling a Delegate inside in Form1 when triggering the event, which initializes the event class configuration. For details, please refer to: delegate and multi-threaded

转载请注明原文地址:https://www.9cbs.com/read-86072.html

New Post(0)