Solved

How to use multi-threading in VB.NET (ASP.NET)

Posted on 2003-11-03
5
1,990 Views
Last Modified: 2007-12-19
I am trying to run a process in the background to do some processing (calling a vb class in my project) when a user hits my aspx page.  I have tested my class independantly of the threading call and it works without error.  I cannot get the code to work with the threading.  I would like the user to be able to go on and use the web-app without having to wait for the process to complete.  Any help appreciated.

the ASPX page (code behind):

Imports System.Threading

Public Class home
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private clsUtility As New SPN.cSysUtility()
    Private clsDBJob As New SPN.cSysDBJob()
    Private thrdDBJob As Thread

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        thrdDBJob = New Thread(New ThreadStart(AddressOf DoDBJob))
        thrdDBJob.IsBackground = False
        thrdDBJob.Priority = ThreadPriority.Highest
        thrdDBJob.Start()
    End Sub

    Private Sub DoDBJob()
        clsDBJob.DoCheckProviders(Session("DBConnString"))
        clsDBJob.DoCheckOpportunities(Session("DBConnString"))
        thrdDBJob.Abort()
    End Sub

End Class

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

the VB CLASS:

'---------------------------------------------------------------------------------------------------------------------------------
' Class Name    : cSysNotify.vb
'---------------------------------------------------------------------------------------------------------------------------------
' Class Description:
' Contains functionality to run scheduled databse jobs; can be multithreaded
'---------------------------------------------------------------------------------------------------------------------------------

Imports System.Data.OleDb
Imports System.Runtime.Serialization


<Serializable()> Public Class cSysDBJob

    Private clsNotify As New SPN.cSysNotify()

    Public Sub DoCheckProviders(ByVal paramConnectionString As String)
     ...
    End Sub

    Public Sub DoCheckOpportunities(ByVal paramConnectionString As String)
     ...
    End Sub
End Class
---------------------------------------------------------------------------------------

0
Comment
Question by:thomasdodds
[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
  • 3
5 Comments
 
LVL 4

Expert Comment

by:NetPointer
ID: 9673647
Why u r aborting this thread??

It will automatically be finished..

Regards,
NetPointer
0
 
LVL 6

Author Comment

by:thomasdodds
ID: 9673663
um ... to be honest that is the evidence of my last attempt ... I thought perhaps that if I could get it to run once, that it might not get cleaned up automatically ... I will remove that line ... but I can't get the code to run - so that is really inconsequential
0
 
LVL 6

Author Comment

by:thomasdodds
ID: 9673674
also - I receive the following when I debug:

An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in Unknown Module.

Additional information: The type System.Web.HttpException in Assembly System.Web, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a is not marked as serializable.

I set my breakpoint on the following:  "Private Sub DoDBJob()" and it errors on: "clsDBJob.DoCheckProviders(Session("DBConnString"))"


0
 
LVL 23

Accepted Solution

by:
naveenkohli earned 500 total points
ID: 9673785
If you need some variables passed into the Thread function, then use SetData. See how slots are used to pass data in.
0
 
LVL 6

Author Comment

by:thomasdodds
ID: 9674051
naveenkohli,

thx for pointing me in the right direction!

This is what works - most likely could use some refinement ... again, a big thank you!

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

ASPX page:

Imports System.Threading

Public Class home
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private clsUtility As New SPN.cSysUtility()
    Private clsDBJob As New SPN.cSysDBJob()
    Private thrdDBJob As Thread

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        'DoDBJob()
        thrdDBJob = New Thread(New ThreadStart(AddressOf DoDBJob))
        thrdDBJob.IsBackground = False
        thrdDBJob.Priority = ThreadPriority.Highest
        thrdDBJob.AllocateNamedDataSlot("DBConnString")
        thrdDBJob.SetData(thrdDBJob.GetNamedDataSlot("DBConnString"), Session("DBConnString"))
        clsDBJob.strConnectionString = thrdDBJob.GetData(thrdDBJob.GetNamedDataSlot("DBConnString"))
        thrdDBJob.FreeNamedDataSlot("DBConnString")
        thrdDBJob.Start()
    End Sub

    Private Sub DoDBJob()
        clsDBJob.DoCheckProviders()
        clsDBJob.DoCheckOpportunities()
    End Sub

End Class

--------------------------------------------------------------------------------------------------------------------------
VB CLASS

'---------------------------------------------------------------------------------------------------------------------------------
' Class Name    : cSysNotify.vb
'---------------------------------------------------------------------------------------------------------------------------------
' Class Description:
' Contains functionality to run scheduled databse jobs; can be multithreaded
'---------------------------------------------------------------------------------------------------------------------------------

Imports System.Data.OleDb
Imports System.Runtime.Serialization
Imports System.Web.SessionState


<Serializable()> Public Class cSysDBJob

    Private clsNotify As New SPN.cSysNotify()
    Public strConnectionString As String

    Public Sub DoCheckProviders()
     ...
    End Sub

    Public Sub DoCheckOpportunities()
     ...
    End Sub
End Class
---------------------------------------------------------------------------------------
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

630 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