Solved

Background Processing in VB

Posted on 1998-10-22
10
464 Views
Last Modified: 2008-02-01
How do you code background processing in VB5?

I am designing an app where part of the app runs some serious mathematical code and takes hours to run.  I would like to place this processing in the background enabling the user to continue using the app.  I tried using an ActiveX EXE but am unable to return control back to the main app.

Can you please provide some example code?
0
Comment
Question by:sadkat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 2

Expert Comment

by:mkmccreary
ID: 1441237
McIx is better at this than I am, but this is how I understand how it works.  You create an object that runs on the server asyncronously.  You create this object where it has an event.  When it is done it raises an event.  The program that called it 'Dim'ed it with events.  So, when the object raises the event, the processing object can process the fact that it is done.  MTS or 'Remote Automation Manager' help you manage remote executing events.

Good Luck,
Martin
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 1441238
Assuming he has a server?
0
 
LVL 14

Expert Comment

by:waty
ID: 1441239
You could launch some threads to do the work.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 4

Expert Comment

by:idcanada
ID: 1441240

Waty is right for the threads,
but what I found quite effective is the good old shell
method.
Shell is multithreaded, therefore pass the parameters
to your exe when you shell it.

Downside, what was 1 app, now becomes 2.
A shells B, B never works alone.

But in your case,
it is not that bad, A will contine to run letting the user
continue while B is calculating.


0
 

Expert Comment

by:sjrl
ID: 1441241
With VB5 it is easy to your own ActiveX objects. These run in a separate thread from the main program. I personally think that the are usually the easiest way to get VB to do multithreading. If you create one that does the lengthy process then you will be able to leave it running. You could even have an event that the object can drive to tell you it has finished. I think this is probably what mkmccreary was referring to. Let me know if you need any help.
0
 

Author Comment

by:sadkat
ID: 1441242
The Shell function is OK, but it does not allow me to pass variables and other infromation to the calculation app.  The variables passed come from the main app and will be different each time the calculation app is called.

Help!!
0
 
LVL 13

Accepted Solution

by:
Mirkwood earned 200 total points
ID: 1441243
Start a timer in your ActiveX server. This timer can be on a invisible form.
So the activeX server has a method DoSomething. DoSomething enables the timer. On the timer event you can do your stuff.
The server can launch an event when it is finished.

Client:
   Dim withevents myServer as MyActiveXServer
 
sub form_load
   set   myServer = new MyActiveServer
end sub

sub command1_click
   myServer.DoSomething
end sub

sub myServer_Finished
   msgbox "Finished"
end sub

server:
event Finished

public sub DoSomething
   timer1.enabled = true  
end sub

sub timer1_click
  timer1.enabled = false
  <Do your stuff>
  raiseevent Finished
end sub

0
 

Author Comment

by:sadkat
ID: 1441244
Thank's for your help so far.  The code is working OK.

BUT....

In the ActiveX server I want to minimize a form (already coded) and show the progress of the calc (% complete like an internet download) in the form's caption.

It seems once the focus is given back to the client I cannot update the ActiveX server form's caption.

Any ideas or code.  Thanks for your help!!
0
 

Author Comment

by:sadkat
ID: 1441245
I'm able to set multiple calls of the calc app.  BUT the calls are working sequentially.  Call 2 must finish before call 1 finishes etc.

Code:

In Client :-

Private Sub mnuRun_Click()
  Dim myServer As TestActiveX.TestDCG
  Set myServer = New TestActiveX.TestDCG
  myServer.DoSomething
End Sub

In Server :-

In Class Module:

Dim frm As New TestForm

Public Sub DoSomething()
  frm.Show vbModeless
  frm.WindowState = vbMinimized
  frm.Timer1.Enabled = True
End Sub

In Form:
Private Sub Timer1_Timer()
  Timer1.Enabled = False
  DoSims
  Unload Me
End Sub

Private Sub DoSims()
  Dim i As Long
  For i = 1 To 10000
    DoEvents
    Caption = i
  Next i
End Sub

0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1441246
The server still has one threat and can only handle one call at the time.

So you cannot start the next DoSomething until the previous one is finished.

Here are some things to think about.
1) Dim myServer As TestActiveX.TestDCG is declared local.
That means that it is immediatelly destroyed after you start DoSomething. Your DoSims is never executed. Make this a global variable.

2) Make sure that the timer is enabled = false and interval = 50
Otherwise your timer is already started before you give the command or is not started at all.

3) Here is a more elaborate example.
---- CLIENT FORM ----
Option Explicit
Dim WithEvents m_myserver As ActiveXExe ' Global declaration of the exe

Private Sub Command1_Click()
    On Error GoTo leave
    m_myserver.DoSomething   'Start exe, second call while busy will raise error
leave:
    Select Case Err.Number
        Case 0
        Case -1
            MsgBox "The server is busy, please wait"
        Case Else
            MsgBox Err.Description
    End Select
End Sub

Private Sub Form_Load()
    Set m_myserver = New ActiveXExe 'Create the server
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Set m_myserver = Nothing 'Destroy the server
End Sub

Private Sub m_myserver_Finished(ByVal OK As Boolean)
    MsgBox IIf(OK, "Finished", "Aborted at " & m_myserver.progress) 'Notify user about server state
End Sub

--- ACTIVEX EXE ---
Option Explicit

Dim WithEvents frm As Form
Dim WithEvents frmtimer As Timer
Dim fBusy As Boolean
Dim m_prg As Long

Public Event Finished(ByVal OK As Boolean)

Public Property Get Progress() As Integer
    Progress = m_prg
End Property

Public Sub DoSomething()
    If (fBusy = True) Then
        Err.Raise -1, , "Exe is Busy"
    End If
    fBusy = True
    frm.Show vbModeless
    frm.WindowState = vbMinimized
    frmtimer.Enabled = True
End Sub


Private Sub Class_Initialize()
    m_prg = 0
    Set frm = New Form1
    Set frmtimer = frm.Timer1
End Sub

Private Sub Class_Terminate()
    Unload frm
    Set frmtimer = Nothing
    Set frm = Nothing
End Sub


Private Sub frm_Unload(Cancel As Integer)
    RaiseEvent Finished(Not fBusy) 'raise event
    If (fBusy) Then fBusy = False  ' abort
    m_prg = 0
End Sub

Private Sub frmtimer_Timer()
    frmtimer.Enabled = False
    DoSims
    Unload frm
End Sub

Private Sub DoSims()
    For m_prg = 1 To 10000
        DoEvents
        If Not fBusy Then Exit Sub 'aborted
        frm.Label1.Caption = m_prg
    Next m_prg
    fBusy = False
    m_prg = 0
End Sub



0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question