I made a VS2003 Macro that will put boilerplate code and prompt for
values. It's not perfect since I been using VS12005 for a while I
haven't used it lately in favor of snippets. Also be aware that the
Callback method will be running on a background thread. So if you're
doing a Windows form application you must marshall to the thread of the
UI before doing anything to objects or data that derive from Control.
Under the MakeCallback macro is a macro for a method to Marshall itself.
This code was originally based on some code from CodeProject
Sub MakeCallback()
Dim rName As String = ""
Dim pad As String = ""
Dim junk As String
Dim count, i As Integer
Dim startpoint, endpoint, tmppoint As EditPoint
Dim methodName As String = ""
Dim delegateName As String = ""
Dim marshalToMethod As String = ""
methodName = InputBox("methodName:", "methodName")
delegateName = InputBox("delegateName:", "delegateName")
marshalToMethod = InputBox("marshalToMethod:",
"marshalToMethod")
With DTE.ActiveDocument.Selection
startpoint = .TopPoint.CreateEditPoint()
endpoint = .BottomPoint.CreateEditPoint
End With
'ELR: ADDED THIS, to move the startpoint to the start of the
line
'so that the Pad function works correctly
If Not startpoint.AtStartOfLine Then
startpoint.StartOfLine()
End If
DTE.UndoContext.Open("Insert A Callback")
Try
'ELR: ADDED Test for Languages
If DTE.ActiveDocument.Language = "CSharp" Then
' C Sharp Code
startpoint.Insert(String.Format("private void
{0}(IAsyncResult aResult){1}", methodName, vbCrLf))
startpoint.Insert(String.Format(" {{{0}", vbCrLf))
startpoint.Insert(String.Format(" AsyncResult
theResult = (AsyncResult)aResult;{0}", vbCrLf))
startpoint.Insert(String.Format("
if(theResult.EndInvokeCalled == false){0}", vbCrLf))
startpoint.Insert(String.Format(" {{{0}", vbCrLf))
startpoint.Insert(String.Format(" {0} cDel =
({0})theResult.AsyncDelegate;{1}", delegateName, vbCrLf))
startpoint.Insert(String.Format(" try{0}
{{{0} cDel.EndInvoke(aResult);{0}", vbCrLf))
If marshalToMethod.Length > 1 Then
startpoint.Insert(String.Format("
{0}();{1}", marshalToMethod, vbCrLf))
End If
startpoint.Insert(String.Format(" }}{0}",
vbCrLf))
startpoint.Insert(String.Format(" catch(Exception
ex){0} {{{0}
System.Diagnostics.Debug.WriteLine(ex.Message);{0} }}{0}",
vbCrLf))
startpoint.Insert(String.Format(" }}{0}", vbCrLf))
startpoint.Insert(String.Format(" }}{0}", vbCrLf))
End If
Finally
DTE.UndoContext.Close()
End Try
End Sub
Shared Sub MakeInvoke()
Dim rName As String = ""
Dim pad As String = ""
Dim junk As String
Dim count, i As Integer
Dim startpoint, endpoint, tmppoint As EditPoint
Dim returnType As String = ""
Dim methodName As String = ""
Dim param1Type As String = ""
Dim param1Name As String = ""
returnType = InputBox("ReturnType:", "ReturnType")
methodName = InputBox("methodName:", "methodName")
param1Type = InputBox("param1Type: (one space for none)",
"param1Type")
param1Name = InputBox("param1Name: (one space for none)",
"param1Name")
With DTE.ActiveDocument.Selection
startpoint = .TopPoint.CreateEditPoint()
endpoint = .BottomPoint.CreateEditPoint
End With
If Not startpoint.AtStartOfLine Then
startpoint.StartOfLine()
End If
DTE.UndoContext.Open("Insert A Invoke")
Try
If DTE.ActiveDocument.Language = "CSharp" Then
' C Sharp Code
startpoint.Insert(String.Format("private delegate
{0} {1}Delegate({2} {3});{4}", returnType, methodName, param1Type,
param1Name, vbCrLf))
startpoint.Insert(String.Format(" private {0}
{1}({2} {3}){4}", returnType, methodName, param1Type, param1Name,
vbCrLf))
startpoint.Insert(String.Format(" {{{0}", vbCrLf))
startpoint.Insert(String.Format("
if(this.InvokeRequired){0}", vbCrLf))
startpoint.Insert(String.Format(" {{{0}",
vbCrLf))
startpoint.Insert(String.Format("
{0}Delegate del = new {0}Delegate(this.{0});{1}", methodName, vbCrLf))
startpoint.Insert(String.Format(" object[]
oArgs = new object[]{{{0}}};{1}", param1Name, vbCrLf))
startpoint.Insert(String.Format("
this.BeginInvoke(del,oArgs);{0}", vbCrLf))
startpoint.Insert(String.Format(" }}{0}",
vbCrLf))
startpoint.Insert(String.Format(" else{0}",
vbCrLf))
startpoint.Insert(String.Format(" {{{0}",
vbCrLf))
startpoint.Insert(String.Format(" {0}",
vbCrLf))
startpoint.Insert(String.Format(" }}{0}",
vbCrLf))
startpoint.Insert(String.Format(" }}{0}", vbCrLf))
End If
Finally
DTE.UndoContext.Close()
End Try
End Sub
Regards,
J. Vince Pacella / OOCL Chicago
Cell 773-454-8683 Fax - 773-867-5050
Cargo Tracking Online at:
www.cargosmart.com
> -----Original Message-----
> From: Discussion of advanced .NET topics.
> [mailto:[EMAIL PROTECTED] On Behalf Of
> Ragnvald Barth
> Sent: Thursday, October 20, 2005 1:42
> To: [email protected]
> Subject: [ADVANCED-DOTNET] How do I implement the
> IAsynchResult design pattern?
>
> I have a method MyMethod(), and I want to provide an
> asynchronous alternative, following the so called
> IAsynchResult design pattern.
> ms-
> help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxadv
> ance/html/c9b
> 3501e-6bc6-40f9-8efd-4b6d9e39ccf0.htm
>
> So I create two new methods: BeginMyMethod, and EndMyMethod.
> The signatures are given from the pattern, but how do I go
> about the implementation? I know I can write code that
> behaves as the pattern describes, but I also know that this
> must have been done before... Can anybody give me an
> explanation, or point me to an article or an example?
>
> ===================================
> This list is hosted by DevelopMentor(r) http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>
>
>
IMPORTANT NOTICE
Email from OOCL is confidential and may be legally privileged. If it is not
intended for you, please delete it immediately unread. The internet cannot
guarantee that this communication is free of viruses, interception or
interference and anyone who communicates with us by email is taken to accept
the risks in so doing. Without limitation, OOCL and its affiliates accept no
liability whatsoever and howsoever arising in connection with the use of this
email. Under no circumstances shall this email constitute a binding agreement
to carry or for provision of carriage services by OOCL, which is subject to the
availability of carrier's equipment and vessels and the terms and conditions of
OOCL's standard bill of lading which is also available at http://www.oocl.com.
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com