thomasdodds
asked on
How to use multi-threading in VB.NET (ASP.NET)
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.Debugg erStepThro ugh()> 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("D BConnStrin g"))
clsDBJob.DoCheckOpportunit ies(Sessio n("DBConnS tring"))
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.Serializati on
<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
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- -
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.Debugg
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(
clsDBJob.DoCheckOpportunit
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.Serializati
<Serializable()> Public Class cSysDBJob
Private clsNotify As New SPN.cSysNotify()
Public Sub DoCheckProviders(ByVal paramConnectionString As String)
...
End Sub
Public Sub DoCheckOpportunities(ByVal
...
End Sub
End Class
--------------------------
ASKER
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
ASKER
also - I receive the following when I debug:
An unhandled exception of type 'System.Runtime.Serializat ion.Serial izationExc eption' occurred in Unknown Module.
Additional information: The type System.Web.HttpException in Assembly System.Web, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d 50a3a is not marked as serializable.
I set my breakpoint on the following: "Private Sub DoDBJob()" and it errors on: "clsDBJob.DoCheckProviders (Session(" DBConnStri ng"))"
An unhandled exception of type 'System.Runtime.Serializat
Additional information: The type System.Web.HttpException in Assembly System.Web, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d
I set my breakpoint on the following: "Private Sub DoDBJob()" and it errors on: "clsDBJob.DoCheckProviders
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.Debugg erStepThro ugh()> 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.AllocateNamedDat aSlot("DBC onnString" )
thrdDBJob.SetData(thrdDBJo b.GetNamed DataSlot(" DBConnStri ng"), Session("DBConnString"))
clsDBJob.strConnectionStri ng = thrdDBJob.GetData(thrdDBJo b.GetNamed DataSlot(" DBConnStri ng"))
thrdDBJob.FreeNamedDataSlo t("DBConnS tring")
thrdDBJob.Start()
End Sub
Private Sub DoDBJob()
clsDBJob.DoCheckProviders( )
clsDBJob.DoCheckOpportunit ies()
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.Serializati on
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
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- -
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.Debugg
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.AllocateNamedDat
thrdDBJob.SetData(thrdDBJo
clsDBJob.strConnectionStri
thrdDBJob.FreeNamedDataSlo
thrdDBJob.Start()
End Sub
Private Sub DoDBJob()
clsDBJob.DoCheckProviders(
clsDBJob.DoCheckOpportunit
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.Serializati
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
--------------------------
It will automatically be finished..
Regards,
NetPointer