DLL, THREAD and I can not stop it

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I created application that I get information from AS400 for reporting.
In main.exe has only 1 frm which calls (as a class library) CLS_MAIN.dll.
The CLS_MAIN.dll get the tables from AS400 and stores it in Dataset.
Then returns to main frm then main frm calls the FirstReport.dll.
The FirstReport.dll get the information from access database and creates the
sqkl string and works with dataset and then writes in pre-formated Excel
sheet.

I create main_thread in main form(frm) that calls for CLS_MAIN(dll), then
FirstReport(dll) and WriteInEXCELL(dll).

Now while is doing this I createted cancel button. Main purpose of this that
user can be able to cancel creating first report and can goto second report.

But I cannot be able to stoping the thread. When I use serverThread.Abort()
it goes to main form and when user try to create second Report gets exepction
that Thread did not stoped or cannot create same thread again.

I want to cancel the thread then user can send different parameter so she or
he can be able to create different report.

I learn that I should use the serverThread.Join() after the calling the
serverThread.Abort(). But I don't know how to applie to my application.

I also like to know how to unload the class(dll's).

I thank you in advance to helping me out.

Rgds,
Niyazi
 
Niyazi,

Not 100% sure I got your question...
Here are my 2 swiss cents:

When calling thread.abort, this will raise an abortException in the thread.
It is up to you to catch it so as to perform clean up.
If I recall correctly, you cannot reuse your instance of the thread that was
previouly aborted. You have to get a fresh one and call the start method.

- José
 
Hi Jose,
Thank you for info.
When I catch the exeption then what should I do, without killing main.exe?
The thread reside in main.exe. Also how to create fresh thread?

In frmMain I use this code:
'PRIVATE MEMBERS
'Server Thread
Private serverThread As New Thread(AddressOf _MAIN_THREAD)

When user clicks the button does some userinterface changes and then call the
Private Sub playAnime() that shows Macromedia Flash loading, creating anime
and then calls the tread as:

If Panel1.Visible = True Then
serverThread.Start()
End If

and then in _MAIN_THREAD I have this:

'MAIN THREAD
Sub _MAIN_THREAD()
'FOR TBAccess.mdb
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Try
'FOR DATASET
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
‘clsConst, clsDSFill in the clsMAIN class library

If clsConst.m_DSLoaded = False Then
Try
'Call DATASET fill class
Dim callDS As New clsDSFill("I am calling")
Catch ex As Exception
MsgBox("ex.Message.ToString)
End Try
Else
'Load the data from AS400 from first report,
'When user try to run second report onward application
'will use the loaded data from Data set
End If




'ALL CLASSES HAS TO BE CALLED IN HERE
++++++++++++++++++++++++++++++++++++++++++
‘[clsBL100A_TableName -> clsReadAccNo -> clsDataFromAS400]

Try
'READ ACCOUNT NO from MS ACCESS DB to GET Information (SUM) from
AS400
Dim tableSize As Integer
If clsConst.checkedBox = "BL100A" Then
For tableSize = 1 To 46
'Call CLASS LOOP [ clsBL100A_TableName -> clsReadAccNo ->
clsDataFromAS400 ]
Dim _CALL_TABLE_NAME As New
BL100A.clsBL100A_TableName(CStr(tableSize))
Next
ElseIf clsConst.checkedBox = "KZ100A" Then
For tableSize = 1 To 33
'Call CLASS LOOP [ clsKZ100A_TableName -> clsReadAccNo ->
clsDataFromAS400 ]
Dim _CALL_TABLE_NAME As New
KZ100A.clsKZ100A_TableName(CStr(tableSize))
Next
ElseIf ……………….
‘………………………
End If
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
‘++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



'WRITING/CREATING (IN) NEW EXCEL SHEET
++++++++++++++++++++++++++++++++++++++++
Try
If clsConst.checkedBox = "BL100A" Then
'Create EXCEL REPORT for BL100A
Dim KEMAL As New BL100A.clsWINEXCEL
ElseIf clsConst.checkedBox = "KZ100A" Then
'Create EXCEL REPORT for KZ100A
Dim KEMAL As New KZ100A.clsWINEXCEL
Else
MsgBox("clsConst.checkedBox does not contain valid data",
MsgBoxStyle.Information)
End If
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try


Thread creates a report maximum 7 min and min 1.30 min.
But if user clicks cancel button I need to kill the thread but not the
main.exe
So user can get different report.

Here is the Cancel button code:

Try
'Stop the SERVER_THREAD
serverThread.Abort()
If clsConst.checkedBox = "BL100A" Then
chkBL100A.Enabled = True
'Show the check mark graphic
pBox1.Visible = False
AxShockwaveFlash1.Stop()
Panel1.Visible = False
ElseIf clsConst.checkedBox = "KZ100A" Then
chkKZ100A.Enabled = True
'Show the check mark graphic
pBox2.Visible = False
AxShockwaveFlash1.Stop()
Panel1.Visible = False
End if

btnENTER.Enabled = True

Catch ex As Exception
MsgBox(ex.Message)
Finally
serverThread.Join()
End Try


As you can see I create the New instance of the Main thread but after I
cancel and try to get other report I get an error. I know when I abort the
thread I have to wait a certain time to thread will be realy dead. So I
should warn the user while the threat aborting that "Wait! Canceling the
......Report".

But having hard time to to code this. Also if I catch the exception How to
perform the clean up.

I realy appriciate your info, and I hope you can help me bit more.

Rgds,
Niyazi
 
Hi Jose,
Thank you for info.
When I catch the exeption then what should I do, without killing main.exe?
The thread reside in main.exe. Also how to create fresh thread?

In frmMain I use this code:
'PRIVATE MEMBERS
'Server Thread
Private serverThread As New Thread(AddressOf _MAIN_THREAD)

When user clicks the button does some userinterface changes and then call the
Private Sub playAnime() that shows Macromedia Flash loading, creating anime
and then calls the tread as:

If Panel1.Visible = True Then
serverThread.Start()
End If

and then in _MAIN_THREAD I have this:

'MAIN THREAD
Sub _MAIN_THREAD()
'FOR TBAccess.mdb
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Try
'FOR DATASET
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
‘clsConst, clsDSFill in the clsMAIN class library

If clsConst.m_DSLoaded = False Then
Try
'Call DATASET fill class
Dim callDS As New clsDSFill("I am calling")
Catch ex As Exception
MsgBox("ex.Message.ToString)
End Try
Else
'Load the data from AS400 from first report,
'When user try to run second report onward application
'will use the loaded data from Data set
End If




'ALL CLASSES HAS TO BE CALLED IN HERE
++++++++++++++++++++++++++++++++++++++++++
‘[clsBL100A_TableName -> clsReadAccNo -> clsDataFromAS400]

Try
'READ ACCOUNT NO from MS ACCESS DB to GET Information (SUM) from
AS400
Dim tableSize As Integer
If clsConst.checkedBox = "BL100A" Then
For tableSize = 1 To 46
'Call CLASS LOOP [ clsBL100A_TableName -> clsReadAccNo ->
clsDataFromAS400 ]
Dim _CALL_TABLE_NAME As New
BL100A.clsBL100A_TableName(CStr(tableSize))
Next
ElseIf clsConst.checkedBox = "KZ100A" Then
For tableSize = 1 To 33
'Call CLASS LOOP [ clsKZ100A_TableName -> clsReadAccNo ->
clsDataFromAS400 ]
Dim _CALL_TABLE_NAME As New
KZ100A.clsKZ100A_TableName(CStr(tableSize))
Next
ElseIf ……………….
‘………………………
End If
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
‘++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



'WRITING/CREATING (IN) NEW EXCEL SHEET
++++++++++++++++++++++++++++++++++++++++
Try
If clsConst.checkedBox = "BL100A" Then
'Create EXCEL REPORT for BL100A
Dim KEMAL As New BL100A.clsWINEXCEL
ElseIf clsConst.checkedBox = "KZ100A" Then
'Create EXCEL REPORT for KZ100A
Dim KEMAL As New KZ100A.clsWINEXCEL
Else
MsgBox("clsConst.checkedBox does not contain valid data",
MsgBoxStyle.Information)
End If
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try


Thread creates a report maximum 7 min and min 1.30 min.
But if user clicks cancel button I need to kill the thread but not the
main.exe
So user can get different report.

Here is the Cancel button code:

Try
'Stop the SERVER_THREAD
serverThread.Abort()
If clsConst.checkedBox = "BL100A" Then
chkBL100A.Enabled = True
'Show the check mark graphic
pBox1.Visible = False
AxShockwaveFlash1.Stop()
Panel1.Visible = False
ElseIf clsConst.checkedBox = "KZ100A" Then
chkKZ100A.Enabled = True
'Show the check mark graphic
pBox2.Visible = False
AxShockwaveFlash1.Stop()
Panel1.Visible = False
End if

btnENTER.Enabled = True

Catch ex As Exception
MsgBox(ex.Message)
Finally
serverThread.Join()
End Try


As you can see I create the New instance of the Main thread but after I
cancel and try to get other report I get an error. I know when I abort the
thread I have to wait a certain time to thread will be realy dead. So I
should warn the user while the threat aborting that "Wait! Canceling the
......Report".

But having hard time to to code this. Also if I catch the exception How to
perform the clean up.

I realy appriciate your info, and I hope you can help me bit more.

Rgds,
Niyazi
 
Back
Top