473,322 Members | 1,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Multithreading

I have an application that monitoring some directories.
When new file arrives my application importing that file into database (very
long process)
I want to rewrite this application to multithread application so if multiple
files arrive at the same time the directory monitor should start new thread
for each file . I don't need any feedback from importing routine (all
information about flow stored into log file).

I am looking for some samle application that have similar functionality or
some online tutorial / article on this subject.

Also I have a question:
What happens to class/method that run on separate thread if starting
applications closed?

Thanks you,

Michael

Nov 21 '05 #1
6 1653
I would create a seperate class to do the import. You will then need to
create some properties in the class for the filename to import etc as you
can't pass these to the thread easily when calling the function...

So for example:

Public Class myFileImport
Dim _Filename as string
Public Property FileName as string
Get
return _FileName
End Get
Set (ByVal Value as string)
_FileName=Value
End Set
End Property
Public Sub ImportFile()
' Put your import code here
End Sub
End Class
Then in your main routine, create the thread as follows:

Dim myFunction as new myFileImport()
Dim mythread as new threading.thread(addressof myFunction.ImportFile)
mythread.start

Hope this helps
Simon

"Michael" <md********@urb.ca> wrote in message
news:O7**************@TK2MSFTNGP09.phx.gbl...
I have an application that monitoring some directories.
When new file arrives my application importing that file into database
(very
long process)
I want to rewrite this application to multithread application so if
multiple
files arrive at the same time the directory monitor should start new
thread
for each file . I don't need any feedback from importing routine (all
information about flow stored into log file).

I am looking for some samle application that have similar functionality or
some online tutorial / article on this subject.

Also I have a question:
What happens to class/method that run on separate thread if starting
applications closed?

Thanks you,

Michael

Nov 21 '05 #2
Thanks Simon

This is exactly what I need.
But my question is still open:
What happens with myFileImport Class when my Main Class ends?
I guess that a new thread will continue to work without any problem, am I
right?
Michael

"Simon Verona" <ne**@aphroditeuk.com> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
I would create a seperate class to do the import. You will then need to
create some properties in the class for the filename to import etc as you
can't pass these to the thread easily when calling the function...

So for example:

Public Class myFileImport
Dim _Filename as string
Public Property FileName as string
Get
return _FileName
End Get
Set (ByVal Value as string)
_FileName=Value
End Set
End Property
Public Sub ImportFile()
' Put your import code here
End Sub
End Class
Then in your main routine, create the thread as follows:

Dim myFunction as new myFileImport()
Dim mythread as new threading.thread(addressof myFunction.ImportFile)
mythread.start

Hope this helps
Simon

"Michael" <md********@urb.ca> wrote in message
news:O7**************@TK2MSFTNGP09.phx.gbl...
I have an application that monitoring some directories.
When new file arrives my application importing that file into database
(very
long process)
I want to rewrite this application to multithread application so if
multiple
files arrive at the same time the directory monitor should start new
thread
for each file . I don't need any feedback from importing routine (all
information about flow stored into log file).

I am looking for some samle application that have similar functionality or some online tutorial / article on this subject.

Also I have a question:
What happens to class/method that run on separate thread if starting
applications closed?

Thanks you,

Michael


Nov 21 '05 #3
No..

If your main application stops, all the threads die with it...
Regards
Simon
"Michael" <md********@urb.ca> wrote in message
news:es**************@TK2MSFTNGP11.phx.gbl...
Thanks Simon

This is exactly what I need.
But my question is still open:
What happens with myFileImport Class when my Main Class ends?
I guess that a new thread will continue to work without any problem, am I
right?
Michael

"Simon Verona" <ne**@aphroditeuk.com> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
I would create a seperate class to do the import. You will then need to
create some properties in the class for the filename to import etc as you
can't pass these to the thread easily when calling the function...

So for example:

Public Class myFileImport
Dim _Filename as string
Public Property FileName as string
Get
return _FileName
End Get
Set (ByVal Value as string)
_FileName=Value
End Set
End Property
Public Sub ImportFile()
' Put your import code here
End Sub
End Class
Then in your main routine, create the thread as follows:

Dim myFunction as new myFileImport()
Dim mythread as new threading.thread(addressof myFunction.ImportFile)
mythread.start

Hope this helps
Simon

"Michael" <md********@urb.ca> wrote in message
news:O7**************@TK2MSFTNGP09.phx.gbl...
>I have an application that monitoring some directories.
> When new file arrives my application importing that file into database
> (very
> long process)
> I want to rewrite this application to multithread application so if
> multiple
> files arrive at the same time the directory monitor should start new
> thread
> for each file . I don't need any feedback from importing routine (all
> information about flow stored into log file).
>
> I am looking for some samle application that have similar functionality or > some online tutorial / article on this subject.
>
> Also I have a question:
> What happens to class/method that run on separate thread if starting
> applications closed?
>
> Thanks you,
>
> Michael
>
>
>



Nov 21 '05 #4
Ok. Looks like I do need feedback to notice that process is finished and
main application is safe to close.
How can I do that? Fire events?
"Simon Verona" <ne**@aphroditeuk.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
No..

If your main application stops, all the threads die with it...
Regards
Simon
"Michael" <md********@urb.ca> wrote in message
news:es**************@TK2MSFTNGP11.phx.gbl...
Thanks Simon

This is exactly what I need.
But my question is still open:
What happens with myFileImport Class when my Main Class ends?
I guess that a new thread will continue to work without any problem, am I right?
Michael

"Simon Verona" <ne**@aphroditeuk.com> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
I would create a seperate class to do the import. You will then need to create some properties in the class for the filename to import etc as you can't pass these to the thread easily when calling the function...

So for example:

Public Class myFileImport
Dim _Filename as string
Public Property FileName as string
Get
return _FileName
End Get
Set (ByVal Value as string)
_FileName=Value
End Set
End Property
Public Sub ImportFile()
' Put your import code here
End Sub
End Class
Then in your main routine, create the thread as follows:

Dim myFunction as new myFileImport()
Dim mythread as new threading.thread(addressof myFunction.ImportFile) mythread.start

Hope this helps
Simon

"Michael" <md********@urb.ca> wrote in message
news:O7**************@TK2MSFTNGP09.phx.gbl...
>I have an application that monitoring some directories.
> When new file arrives my application importing that file into database > (very
> long process)
> I want to rewrite this application to multithread application so if
> multiple
> files arrive at the same time the directory monitor should start new
> thread
> for each file . I don't need any feedback from importing routine (all
> information about flow stored into log file).
>
> I am looking for some samle application that have similar
functionality or
> some online tutorial / article on this subject.
>
> Also I have a question:
> What happens to class/method that run on separate thread if starting
> applications closed?
>
> Thanks you,
>
> Michael
>
>
>



Nov 21 '05 #5
Depends on if you've got a finite no of threads...

"Michael" <md********@urb.ca> wrote in message news:e3**************@TK2MSFTNGP10.phx.gbl...
Ok. Looks like I do need feedback to notice that process is finished and
main application is safe to close.
How can I do that? Fire events?


"Simon Verona" <ne**@aphroditeuk.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
No..

If your main application stops, all the threads die with it...
Regards
Simon
"Michael" <md********@urb.ca> wrote in message
news:es**************@TK2MSFTNGP11.phx.gbl...
> Thanks Simon
>
> This is exactly what I need.
> But my question is still open:
> What happens with myFileImport Class when my Main Class ends?
> I guess that a new thread will continue to work without any problem, am I > right?
>
>
> Michael
>
>
>
> "Simon Verona" <ne**@aphroditeuk.com> wrote in message
> news:ON**************@TK2MSFTNGP12.phx.gbl...
>> I would create a seperate class to do the import. You will then need to >> create some properties in the class for the filename to import etc as you >> can't pass these to the thread easily when calling the function...
>>
>> So for example:
>>
>> Public Class myFileImport
>> Dim _Filename as string
>> Public Property FileName as string
>> Get
>> return _FileName
>> End Get
>> Set (ByVal Value as string)
>> _FileName=Value
>> End Set
>> End Property
>> Public Sub ImportFile()
>> ' Put your import code here
>> End Sub
>> End Class
>>
>>
>> Then in your main routine, create the thread as follows:
>>
>> Dim myFunction as new myFileImport()
>> Dim mythread as new threading.thread(addressof myFunction.ImportFile) >> mythread.start
>>
>> Hope this helps
>> Simon
>>
>> "Michael" <md********@urb.ca> wrote in message
>> news:O7**************@TK2MSFTNGP09.phx.gbl...
>> >I have an application that monitoring some directories.
>> > When new file arrives my application importing that file into database >> > (very
>> > long process)
>> > I want to rewrite this application to multithread application so if
>> > multiple
>> > files arrive at the same time the directory monitor should start new
>> > thread
>> > for each file . I don't need any feedback from importing routine (all
>> > information about flow stored into log file).
>> >
>> > I am looking for some samle application that have similar functionality > or
>> > some online tutorial / article on this subject.
>> >
>> > Also I have a question:
>> > What happens to class/method that run on separate thread if starting
>> > applications closed?
>> >
>> > Thanks you,
>> >
>> > Michael
>> >
>> >
>> >
>>
>>
>
>




In my application, I have a finite no, so I use a collection to store the threads in...

so, I define the collection as a module level variable as:

Dim runningThreads as new collection

Then each time I start a thread up... I add

runningthreads.add(mythread)

to add it to the collection (this goes just before the mythread.start line).
Then you can check that all the threads have closed by enumerating through them and checking the threadstate property... ie..

Dim t as thread
For Each t In runningthreads
If t.ThreadState <> Threading.ThreadState.Stopped Then
' Thread is still running
End if
I suppose you could take care of the fact that you have an ever growing no of threads by tidying them up in a timer.... eg create a timer to run every thirty seconds and then put code like this in the timer:

Dim t as thread
Dim cnt as int32=0
for each t in runningThreads
if t.threadstate=threading.threadstate.stopped then
runningthreads.remove(cnt)
end if
cnt+=1
next
This will remove the completed threads from your collection. There are other more elegant ways of doing this I'm sure - for example, encapsulating the whole thread collection in a class, but I think it will work just fine for you.

Hope this helps

Regards
Simon
Nov 21 '05 #6
Yes, I want to limit processing treads to number but with ability to change that number dynamicaly (.config file or database)
I think about this approach:

If _NumOfThreads< MAX_NUM_OF_THREADS Then
Dim myFunction as new myFileImport()
Dim mythread as new threading.thread(addressof myFunction.ImportFile)
mythread.start
_NumOfThreads +=1
End If
Private Sub SomeFeedbackFunction() Handles ????
_NumOfThreads -=1

End Sub


How to implement feedback?
In VB6 I used to use notification class. Something like that:

Class Notify

Public Event ThreadFinished()

Public Sub OnThreadFinish()

RaiseEvent ThreadFinished

End Sub
End Class
In main application I declare this class with events, create an instance and pass a reference to child Class (in this case to the class in the new thread):

When New Thread is about to finish his job it calls to objNotify.OnThreadFinish

In main App objNotify fires an event.

Will it work with multithreading and VB.net?
Thank you Michael

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:OZ**************@TK2MSFTNGP11.phx.gbl...
Depends on if you've got a finite no of threads...

"Michael" <md********@urb.ca> wrote in message news:e3**************@TK2MSFTNGP10.phx.gbl...
Ok. Looks like I do need feedback to notice that process is finished and
main application is safe to close.
How can I do that? Fire events?


"Simon Verona" <ne**@aphroditeuk.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
No..

If your main application stops, all the threads die with it...
Regards
Simon
"Michael" <md********@urb.ca> wrote in message
news:es**************@TK2MSFTNGP11.phx.gbl...
> Thanks Simon
>
> This is exactly what I need.
> But my question is still open:
> What happens with myFileImport Class when my Main Class ends?
> I guess that a new thread will continue to work without any problem, am I > right?
>
>
> Michael
>
>
>
> "Simon Verona" <ne**@aphroditeuk.com> wrote in message
> news:ON**************@TK2MSFTNGP12.phx.gbl...
>> I would create a seperate class to do the import. You will then need to >> create some properties in the class for the filename to import etc as you >> can't pass these to the thread easily when calling the function...
>>
>> So for example:
>>
>> Public Class myFileImport
>> Dim _Filename as string
>> Public Property FileName as string
>> Get
>> return _FileName
>> End Get
>> Set (ByVal Value as string)
>> _FileName=Value
>> End Set
>> End Property
>> Public Sub ImportFile()
>> ' Put your import code here
>> End Sub
>> End Class
>>
>>
>> Then in your main routine, create the thread as follows:
>>
>> Dim myFunction as new myFileImport()
>> Dim mythread as new threading.thread(addressof myFunction.ImportFile) >> mythread.start
>>
>> Hope this helps
>> Simon
>>
>> "Michael" <md********@urb.ca> wrote in message
>> news:O7**************@TK2MSFTNGP09.phx.gbl...
>> >I have an application that monitoring some directories.
>> > When new file arrives my application importing that file into database >> > (very
>> > long process)
>> > I want to rewrite this application to multithread application so if
>> > multiple
>> > files arrive at the same time the directory monitor should start new
>> > thread
>> > for each file . I don't need any feedback from importing routine (all
>> > information about flow stored into log file).
>> >
>> > I am looking for some samle application that have similar functionality > or
>> > some online tutorial / article on this subject.
>> >
>> > Also I have a question:
>> > What happens to class/method that run on separate thread if starting
>> > applications closed?
>> >
>> > Thanks you,
>> >
>> > Michael
>> >
>> >
>> >
>>
>>
>
>




In my application, I have a finite no, so I use a collection to store the threads in...

so, I define the collection as a module level variable as:

Dim runningThreads as new collection

Then each time I start a thread up... I add

runningthreads.add(mythread)

to add it to the collection (this goes just before the mythread.start line).
Then you can check that all the threads have closed by enumerating through them and checking the threadstate property... ie..

Dim t as thread
For Each t In runningthreads
If t.ThreadState <> Threading.ThreadState.Stopped Then
' Thread is still running
End if
I suppose you could take care of the fact that you have an ever growing no of threads by tidying them up in a timer.... eg create a timer to run every thirty seconds and then put code like this in the timer:

Dim t as thread
Dim cnt as int32=0
for each t in runningThreads
if t.threadstate=threading.threadstate.stopped then
runningthreads.remove(cnt)
end if
cnt+=1
next
This will remove the completed threads from your collection. There are other more elegant ways of doing this I'm sure - for example, encapsulating the whole thread collection in a class, but I think it will work just fine for you.

Hope this helps

Regards
Simon
Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: dixp | last post by:
I'm new to writing multithreaded apps and I have a design question. I have a winforms app and a class which has a method that does processing which is time intensive. I want the user to be able...
47
by: mihai | last post by:
What does the standard say about those two? Is any assurance that the use of STL is thread safe? Have a nice day, Mihai.
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
5
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up...
9
by: tommy | last post by:
hi, i have found a example for multithreading and asp.net http://www.fawcette.com/vsm/2002_11/magazine/features/chester/ i want to speed up my website ... if my website is starting, they...
2
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and...
55
by: Sam | last post by:
Hi, I have a serious issue using multithreading. A sample application showing my issue can be downloaded here: http://graphicsxp.free.fr/WindowsApplication11.zip The problem is that I need to...
5
by: sandy82 | last post by:
Whats actuallly multithreading is ... and how threading and multithreading differ . Can any1 guide how multithreading is used on the Web .. i mean a practical scenario in which u use...
2
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.