Threading issue

G

Guest

Hi,
I have created a thread to execute my task but for some readon the
application still locks up until the task is completed.Here is my sample code


code on button

Private Sub cmd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdImport.Click


Dim t As New Thread(AddressOf ExecuteTask)

t.Priority = ThreadPriority.Normal

t.Start()

End Sub



Ecexute task


Protected Sub ExecuteTask()
Dim sMessage As String
Dim lErr As Long, sSource As String, sDesc As String
oDts = New DTSPkg80.Package2Class


Try

oDts.LoadFromSQLServer("machine", "sa", "",
DTSPkg80.DTSSQLServerStorageFlags.DTSSQLStgFlag_Default, , , , "NewPackage")

For Each oStep In oDts.Steps
oStep.ExecuteInMainThread = True
Next

oDts.Execute()

For Each oStep In oDts.Steps
If oStep.ExecutionResult =
DTSPkg80.DTSStepExecResult.DTSStepExecResult_Failure Then
oStep.GetExecutionErrorInfo(lErr, sSource, sDesc)

sMessage = oStep.Name & " Failed"

Dim itmp As New Windows.Forms.ListViewItem(sMessage)

itmp.ImageIndex = 0

ListView1.Items.Add(itmp)

Else

sMessage = oStep.Name & " Succeeded"

End If
Next

oDts.UnInitialize()

Catch ex As Exception

MsgBox(ex.Message)

Finally

oStep = Nothing
oDts = Nothing


End Try

The program basically executes a package in sql server and displays the
messages (error or success) in a listview control. Wehn I run the program it
still locks up but runs perfect with the listview.

Any ideas?
 
J

Jon Skeet [C# MVP]

Chris said:
I have created a thread to execute my task but for some readon the
application still locks up until the task is completed.Here is my sample code

For one thing, it looks like you're accessing the UI from within your
worker thread, which you mustn't do.

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml

The main UI thread shouldn't be blocked by the code you've got though.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top