请教下面的代码!

Pavel_d 2009-05-13 09:57:39

using System;

using System.Collections.Generic;

using System.Text;

using System.Management;

using System.Net;

namespace testPrintJobArrival

{

public delegate void PrintJobArrived(object sender, ManagementBaseObject printProperties);

public class WMIPrinter

{

ConnectionOptions _co = new ConnectionOptions();

internal static System.Management.ManagementScope _ms = null;

private const string PRINTERJOBS = "SELECT * FROM Win32_PrintJob";

private const string PRINTERS = "SELECT * FROM Win32_Printer";

private const int QUERYINTERVAL = 1;

public PrintJobArrived OnPrintJobArrived = null;

private System.Management.ManagementEventWatcher _printEventWatcher = null;

private ManagementObjectSearcher _moSearchPrintJobs = null;

public WMIPrinter()

{

//Point to machine

_co.EnablePrivileges = true;

_co.Impersonation = ImpersonationLevel.Delegate;

_ms = new System.Management.ManagementScope("\\\\localhost\\root\\cimv2", _co);

_moSearchPrintJobs = new ManagementObjectSearcher(PRINTERJOBS);

}

public void StartPrintWatcher()

{

_printEventWatcher = new System.Management.ManagementEventWatcher();

_printEventWatcher.Query = new System.Management.EventQuery("SELECT * FROM

__InstanceCreationEvent WITHIN " + QUERYINTERVAL + " WHERE TargetInstance ISA \"Win32_PrintJob\"");

string hostName = Dns.GetHostName();

this._printEventWatcher.Scope = new System.Management.ManagementScope("\\\\" + hostName + "\\root\\CIMV2");

_printEventWatcher.EventArrived += new EventArrivedEventHandler(this.Arrived);



_printEventWatcher.Start(); //asynchronous

//_printEventWatcher.WaitForNextEvent(); //synchronous

}

public void StopPrintWatcher()

{

if (_printEventWatcher != null)

_printEventWatcher.Stop();

}

/// <summary>

/// new Print Job has Arrived...

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

public void Arrived(object sender, EventArrivedEventArgs e)

{

foreach (PropertyData pd in e.NewEvent.Properties)

{

ManagementBaseObject mbo = pd.Value as ManagementBaseObject;

if (mbo != null)

{

//mbo.Properties[""]

if (OnPrintJobArrived != null)

OnPrintJobArrived(this, mbo);

}

}

}

}

}




上面delegate 的使用有问题!
...全文
178 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
Pavel_d 2009-05-13
  • 打赏
  • 举报
回复
OnPrintJobArrived(this, mbo);
[这里是怎么调用的,请解释一下??
Pavel_d 2009-05-13
  • 打赏
  • 举报
回复
_printEventWatcher.EventArrived += new EventArrivedEventHandler(this.Arrived);
在Arrived函数中


public void Arrived(object sender, EventArrivedEventArgs e)

{
foreach (PropertyData pd in e.NewEvent.Properties)
{
ManagementBaseObject mbo = pd.Value as ManagementBaseObject;
if (mbo != null)
{
if (OnPrintJobArrived != null)
OnPrintJobArrived(this, mbo);
这里是怎么调用的,请解释一下??
}
}
}

kangbo818 2009-05-13
  • 打赏
  • 举报
回复
学习,顶
wlzx21 2009-05-13
  • 打赏
  • 举报
回复
关注
Pavel_d 2009-05-13
  • 打赏
  • 举报
回复
这段代码我是从网上搜到的,正在看
_printEventWatcher.EventArrived += new EventArrivedEventHandler(this.Arrived);

这句有哪位请解释一下!!
g2b2 2009-05-13
  • 打赏
  • 举报
回复
是不是要 new 一下
Pavel_d 2009-05-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zgke 的回复:]
WMI 打印机.......
[/Quote]

zgke 做过吗??

打印事件检测不到啊???
光宇广贞 2009-05-13
  • 打赏
  • 举报
回复
在提出有关delegate 的问题之前,请先了解C里面的函数指针类型的用法……放到C#中,换了个叫法,叫做delegate
gyouyang 2009-05-13
  • 打赏
  • 举报
回复
板凳
Pavel_d 2009-05-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zgke 的回复:]
WMI 打印机.......
[/Quote]

zgke 做过吗??

事件检测不到啊。??
xiulinwang123 2009-05-13
  • 打赏
  • 举报
回复
什么问题啊
glboy12 2009-05-13
  • 打赏
  • 举报
回复
具体什么问题 贴出来啊
CSharpEx 2009-05-13
  • 打赏
  • 举报
回复
up
zgke 2009-05-13
  • 打赏
  • 举报
回复
WMI 打印机.......
ErnestChen2go 2009-05-13
  • 打赏
  • 举报
回复
me没时间看了。帮顶!
Pavel_d 2009-05-13
  • 打赏
  • 举报
回复
自己先顶一下!
jwangyx 2009-05-13
  • 打赏
  • 举报
回复
delegate 是委托 建议装个msdn 看看一些用法
wuyq11 2009-05-13
  • 打赏
  • 举报
回复
挂接事件的处理方法
事件预订和处理
Pavel_d 2009-05-13
  • 打赏
  • 举报
回复
EventArrivedEventArgs e 里面那个属性是表示当前事件的??
meheartfly 2009-05-13
  • 打赏
  • 举报
回复
我顶阿!
加载更多回复(2)

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧