找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1944|回复: 0

[分享] 使用AutoCAD .NET API设置 timer

[复制链接]

已领礼包: 6个

财富等级: 恭喜发财

发表于 2013-7-29 11:33:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×


If you want your AutoCAD .NET plug-in to perform some tasks at regular intervals, then it is important to ensure that the timer runs in the AutoCAD's main UI thread. Implementing the timer in a separate thread does not work because AutoCAD (atleast until 2013 release) does not support multithreaded applications.

To setup a timer to work properly, one possible way is to use a hidden modeless window that in-turn instantiates a timer (use the System.Windows.Forms.Timer). The System.Windows.Forms.Timer differs from the other timers provided by the .Net framework and is guranteed to run in the same UI thread which started it. For a comparison of the various timers available in the .Net framework, you may refer to this MSDN magazine article.

Here is the sample code that displays a tray bubble at a specified time interval :

First lets look at the commands class
  1. Imports Autodesk.AutoCAD.Runtime
  2. Imports Autodesk.AutoCAD.ApplicationServices
  3. Imports Autodesk.AutoCAD.DatabaseServices
  4. Imports Autodesk.AutoCAD.EditorInput
  5. Imports Autodesk.AutoCAD.Windows

  6. <Assembly: CommandClass(GetType(AdskMyTimerClass))>

  7. Public Class AdskMyTimerClass
  8.     Implements IExtensionApplication

  9.     'System.Threading.Timer is a simple, lightweight timer that
  10.     'uses callback methods and is served by thread pool threads.
  11.     'It is not recommended for use with Windows Forms, because
  12.     'its callbacks do not occur on the user interface thread.

  13.     'System.Windows.Forms.Timer is a better choice for use with
  14.     'Windows Forms and its callbacks occur on the same UI thread.
  15.     Private _timerForm As MyForm

  16.     Public Sub Initialize() _
  17.         Implements IExtensionApplication.Initialize
  18.         _timerForm = Nothing
  19.     End Sub

  20.     Public Sub Terminate() _
  21.         Implements IExtensionApplication.Terminate
  22.         StopTimerMethod()
  23.     End Sub

  24.     <CommandMethod("StartTimer")> _
  25.     Public Sub StartTimerMethod()
  26.         If _timerForm Is Nothing Then
  27.             _timerForm = New MyForm
  28.             _timerForm.Show()
  29.             _timerForm.Hide()
  30.         End If
  31.     End Sub

  32.     <CommandMethod("StopTimer")> _
  33.     Public Sub StopTimerMethod()
  34.         If _timerForm IsNot Nothing Then
  35.             _timerForm.Close()
  36.             _timerForm.Dispose()
  37.             _timerForm = Nothing
  38.         End If
  39.     End Sub
  40. End Class
  41. Now for the Form class that creates the timer :

  42. Imports Autodesk.AutoCAD.Runtime
  43. Imports Autodesk.AutoCAD.ApplicationServices
  44. Imports Autodesk.AutoCAD.DatabaseServices
  45. Imports Autodesk.AutoCAD.EditorInput
  46. Imports Autodesk.AutoCAD.Windows

  47. Public Class MyForm

  48.     Private _myTimer As System.Windows.Forms.Timer
  49.     Private _ti As TrayItem
  50.     Private _icon As System.Drawing.Icon
  51.     Private _cnt As Int32

  52.     Private Sub MyForm_Load( _
  53.             ByVal sender As System.Object, _
  54.          ByVal e As System.EventArgs) _
  55.          Handles MyBase.Load
  56.         _myTimer = New System.Windows.Forms.Timer()
  57.         _myTimer.Enabled = True
  58.         _myTimer.Interval = 5000
  59.         AddHandler _myTimer.Tick, AddressOf MyBubbleTimer_Tick
  60.         _myTimer.Start()
  61.     End Sub

  62.     Private Sub MyBubbleTimer_Tick( _
  63.             ByVal sender As System.Object, _
  64.             ByVal e As System.EventArgs
  65.                                     )
  66.         _cnt = _cnt + 1

  67.         Dim sb As StatusBar = Application.StatusBar
  68.         Dim tis As TrayItemCollection = sb.TrayItems

  69.         If _ti Is Nothing Then
  70.             _cnt = 1
  71.             _ti = New TrayItem()
  72.             _ti.Visible = True
  73.             _icon = New System.Drawing.Icon("C:\\Temp\\spintest.ico")
  74.             _ti.Icon = Icon
  75.             _ti.ToolTipText = "My Timer trigerred bubble !"
  76.             tis.Add(_ti)
  77.             sb.Update()
  78.         End If

  79.         _ti.CloseBubbleWindows()

  80.         Dim bubble As New TrayItemBubbleWindow()
  81.         bubble.Text = "Hello !"
  82.         bubble.Title = String.Format("{0}", _cnt)
  83.         bubble.HyperLink = "http://www.autodesk.com"
  84.         bubble.HyperText = "Autodesk"

  85.         _ti.ShowBubbleWindow(bubble)
  86.     End Sub

  87.     Private Sub MyForm_FormClosed( _
  88.             ByVal sender As System.Object, _
  89.             ByVal e As System.Windows.Forms.FormClosedEventArgs _
  90.             ) Handles MyBase.FormClosed
  91.         ' Stop the timer
  92.         _myTimer.Stop()
  93.         _myTimer.Dispose()

  94.         ' Remove the tray item
  95.         If _ti IsNot Nothing Then
  96.             Dim sb As StatusBar = Application.StatusBar
  97.             Dim tis As TrayItemCollection = sb.TrayItems
  98.             tis.Remove(_ti)
  99.             sb.Update()
  100.         End If
  101.     End Sub
  102. End Class

评分

参与人数 1D豆 +3 收起 理由
ScmTools + 3 资料分享奖!

查看全部评分

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-17 22:30 , Processed in 0.191985 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表