Re: Posting an event

From: Trev Hunter (hunter_trev_at_hotmail.com)
Date: 02/15/04


Date: Sun, 15 Feb 2004 20:00:21 -0000

Jeff,

If you want to do asynchronous processing, look up the following topics in
the help files and in MSDN

Threading
Thread Pool
Asynchronous delegates

The following links may also be of use:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconFreeThreadingExample.asp

http://msdn.microsoft.com/msdnmag/issues/01/07/vbnet/default.aspx

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbref/html/vbsamwindowsformsmultithreadedtciiplistenersample.asp

Be careful with threads and windows forms - always remember the golden
rule - you shouldn't call a method on a form from a different thread - use
Form.Invoke to call it instead.

If you want your application to process windows messages before executing a
piece of code, call System.Windows.Forms.Application.DoEvents(). DoEvents
processes waiting messages before continuing. InVB6 it was used to allow
your window to stay responsive while preforming background processing. In
VB.net, the addition of threading has become the preferred method for
background processing, but DoEvents is still available.

Below is a quick example of how to call a method asynchronously:

-------------------------

' Delegate to call async
Private Delegate Sub DoStuffDelegate(Byval MyParam as String)

' Main Entry point
Private Sub Main()

    ' Local Variables
    Dim DoStuffAsync as new DoStuffDelegate(AddressOf Me.DoStuff)
    Dim objResult as IAsyncResult

    ' Start the async processing
    objResult = DoStuffAsync.BeginInvoke("test", nothing, nothing)

    ' Do some stuff in this thread
    Threading.Thread.Sleep(5000)

    ' Pause until the processing has finished
    DoStuffAsync.EndInvoke(objResult)

End Sub

' Method that runs on a different thread
Private Sub DoStuff(Byval Param as String)

    ' Do stuff on a different thread
    Threading.Thread.Sleep(10000)

End Sub

-------------------------

Hope this helps,

Trev.

"Jeff Casbeer" <jeffcasbeer@hsi.com> wrote in message
news:5BD95E99-4B84-4396-B938-11488B35564A@microsoft.com...
> New to VB...
>
> What is the VB syntax for posting a Windows event? For example, to have
an event fire AFTER a form loads, I'd add a posted call to a "post_load"
event, from "load". What is the VB syntax for that deferred call?
Raiseevent seems to be synchronous, as does just calling the handler
subroutine. Also, how do I defer processing to allow windows to process
incomplete events, eg., painting?
>
> Thanks - Jeff



Relevant Pages

  • Asynchronous Threading Issue
    ... 12:48:12 PM: 3932: Calling Test1 asnchronously ... Private Delegate Sub Test1Delegate() ... Private Sub Test1Callback ...
    (microsoft.public.dotnet.general)
  • Using Multithreading with Controls (Control.Invoke, VB.NET)
    ... Me.Invoke by a direct call to the delegate. ... Private Delegate Sub myDelegate() ... Private buffer as String ... Private Sub HandleThread1() ...
    (microsoft.public.dotnet.framework.compactframework)
  • Structures and Delegates and ByRef Arguments
    ... If I have a structure and I pass it ByRef to a Subroutine via a Delegate, ... Delegate Sub delegTestSub(ByRef si As SiteRunOpts) ... Private Sub Form1_Load(ByVal sender As System.Object, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How do I know when a thread has finished?
    ... 'The Delegate class is not a delegate type; it is used to derive delegate ... Private Delegate Sub frm2Handler ... Private Sub Form1_Load(ByVal sender As Object, ...
    (microsoft.public.dotnet.languages.vb)
  • Windows 98 problem
    ... and windows installer 2 on the windows 98 machine. ... Private Sub btnQuickQuoteOK_Click(ByVal sender As System.Object, ... Dim caller As New AsyncMethodCaller(AddressOf ...
    (microsoft.public.dotnet.languages.vb)