How to interupt code execution in a method

  • Thread starter Thread starter Frank
  • Start date Start date
F

Frank

I need to make a program that will import data from a
text file into a database.
I have already designed the user interface, and I am
coding a class to open the file and handle the data. Now
an object of the class will be initialised and I will
call the data procedure in it though a method, and pass
it ref parameter for update of a counter in the user
interface, when the user press a start button. All that
seems fine.

However, if the user needs to stop the operation, I need
to somehow be able to break the import. I guess that I
could have a stop button, which could call another method
in the object, that would set a private cancel variable
for the object to the value true. The running import
procedure could then check on the cancel variable.
However, would that ever work, since the first method
that is doing the import, is not complete yet?

Is this a situation where another thread needs to be
used? I have not been working with threads before, so if
this is the case, perhaps someone can explain a bit, and
direct me to some articles in the help system that will
clarify this.


With kind regards,

Frank
 
Hi Crirus,

There is no attachment the posting. I don't think you can
attach to newsgroup postings.
Perhaps you can include a link to a webpage instead.


Regards,

Frank
 
Hi Crisus,

Ok, I won't argue with you there :-).
However, I access the newsgroups through the MSDN website
(http://msdn.microsoft.com/newsgroups/) and I think they
strip all attachments for security reasons. Also, I
access the newsgroups from more than one computer, so
prefer to read them this way).

Perhaps you could be so kind as to direct me to a
webpage, or perhaps there is no such?


With kind regards,

Frank
 
Hi Frank,

I use OE too, so the attachment's fine.
Here's Crirus's code.

Regards,
Fergus

ps. This uses a TextBox for demonstation which is fine here, but is not the
recommended way to interact with UI Controls. For that you need to call
oControl.BeginInvoke.

===========================================
<ThreadWorker.vb>
Imports System.Threading

Public Class ThreadWorker

' members...
Public TextBox As TextBox
Private _thread As Thread

' Start - this method is the entry-point for the thread...
Private Sub Start()

' just set some text on the textbox...
TextBox.Text = "Hello, world from thread " & _
_thread.Name & " #" & _
Thread.CurrentThread.GetHashCode() & "!"
End Sub

' SpinUp - this method is called by the main application thread...
Public Sub SpinUp()

' create a thread start object that refers to our worker...
Dim threadStart As ThreadStart
threadStart = New ThreadStart(AddressOf Me.Start)

' now, create the thread object and start it...
_thread = New Thread(threadStart)
_thread.Name = "Worker"
_thread.Start()

End Sub

End Class
</ThreadWorker.vb>

<Form1.vb>
Imports System.Threading

Add a TextBox - txtResult
Add a Button - btnStartThread

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

Thread.CurrentThread.Name = "Main"
Me.Text &= " - Thread Main #" & Thread.CurrentThread.GetHashCode
End Sub

Private Sub btnStartThread_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnStartThread.Click

' create a new worker...
Dim worker As New ThreadWorker()
worker.TextBox = txtResult

' spin up the thread...
worker.SpinUp()
End Sub
</Form1.vb>
 
Hi Crirus,

ROFL. Fingers in many pies, but in some I'm the bad guy!! I'm not all
sweetness and light, much to the occasional annoyance of a couple of people
and permanent (perhaps not, that's a long time) distaste of a couple of
others.

But more friends than otherwise. ;-))

Regards,
Fergus
 
Back
Top