multiple threads from same sub

  • Thread starter Thread starter whiggins
  • Start date Start date
W

whiggins

Hello
I am writing a VB.net windows form that monitors files bing dropped in to a
directory and then parsing the files out and inserting the contents in to a
SQL database. I have a class that is launched off on it's own thread that
has a callback to the main form to update a listbox that contains the files
that are dropped into the directory. I also have a timer the is set to
elapse every 5 seconds or so. In the timer elapsed sub I want to launch a
thread of class INVIThread to parse the files and insert the data. I would
set up an array for class but it has a withevents declaration. I have add
the code that is in the sub below.

I have set up an array of threads and in the sub I increment the threads on
every call to the sub, but on the second go through I get the error:
"Delegate to an instance method cannot have null 'this'."

Any help would be greatly appreciated.

t(Threadcount) = New Thread(AddressOf INVIThread.StartInsert) error line
INVIThread = New InventoryInsert.Inserter
Dim Filename As String = ""
Filename = lbFiles.Items.Item(0).ToString()
INVIThread.FileName = Filename
INVIThread.FilePath = "D:\inventory\"
t(Threadcount).IsBackground = True
t(Threadcount).Name = Mid(Filename, 1, Filename.Length - 4)
t(Threadcount).Start()
Threadcount += 1
 
[...]
I have set up an array of threads and in the sub I increment the threads
on
every call to the sub, but on the second go through I get the error:
"Delegate to an instance method cannot have null 'this'."

I don't understand the code well enough, not being familiar enough with
VB. But I am suspicious of you using "INVIThread.StartInsert" as your
delegate method, and your assigning "New InventoryInsert.Inserter" to the
"INVIThread" identifier, whatever that is. In particular, I see a couple
of possibilities:

1) "INVIThread" is an actual class name. If so, the error message
implies to me that INVIThread.StartInsert is an instance method, in which
case you should be creating an instance of INVIThread and using the
reference to that instance for the operand of the "AddressOf". In this
case, I don't know what assigning some new value to the class name itself
would do in VB, but I doubt it's good.

2) "INVIThread" is the name of a variable, assigned to some class
named something else. In which case I don't understand why you would
initialize the variable on the line _after_ the line where it needs a
valid reference.

In either case, I suspect the line after the line throwing the error is
somehow related to the problem. What that might be, I don't know. I've
found that VB's design that allows the programmer to treat a classname the
same as a reference to that class in certain situations just obfuscates
buggy code and makes it harder to figure out problems. This could well be
yet another example of that.

In any case, I suspect that you might get better help posting your
question to a VB-specific newsgroup.

I also would recommend you creating a concise-but-complete sample of code
that reliably reproduces the problem. Most people complain "my code is
too complicated to do that!" but that's just a lazy response and in any
case, whether it's difficult or not, you can't get a really good answer if
nobody can see the entire context in which the error happens.

Finally: I don't understand why you have a timer used to create a new
thread every so often. At the very least you should be just queuing a
work item to a thread pool thread, and you could even just create a single
thread that never exits, but instead loops with a 5-second delay at the
beginning of the loop. Doing the latter way could even change your code
enough that whatever bug you've got, it just disappears when the code is
rewritten. :)

Pete
 
Have you tried FileSystemWatcher class. Its better then writing your own
Thread Routine.

FileSystemWatcher class listens to the file system change notifications and
raises events when a directory, or file in a directory, changes.
http://msdn2.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx


Regards
JIGNESH


Peter Duniho said:
[...]
I have set up an array of threads and in the sub I increment the threads
on
every call to the sub, but on the second go through I get the error:
"Delegate to an instance method cannot have null 'this'."

I don't understand the code well enough, not being familiar enough with
VB. But I am suspicious of you using "INVIThread.StartInsert" as your
delegate method, and your assigning "New InventoryInsert.Inserter" to the
"INVIThread" identifier, whatever that is. In particular, I see a couple
of possibilities:

1) "INVIThread" is an actual class name. If so, the error message
implies to me that INVIThread.StartInsert is an instance method, in which
case you should be creating an instance of INVIThread and using the
reference to that instance for the operand of the "AddressOf". In this
case, I don't know what assigning some new value to the class name itself
would do in VB, but I doubt it's good.

2) "INVIThread" is the name of a variable, assigned to some class
named something else. In which case I don't understand why you would
initialize the variable on the line _after_ the line where it needs a
valid reference.

In either case, I suspect the line after the line throwing the error is
somehow related to the problem. What that might be, I don't know. I've
found that VB's design that allows the programmer to treat a classname the
same as a reference to that class in certain situations just obfuscates
buggy code and makes it harder to figure out problems. This could well be
yet another example of that.

In any case, I suspect that you might get better help posting your
question to a VB-specific newsgroup.

I also would recommend you creating a concise-but-complete sample of code
that reliably reproduces the problem. Most people complain "my code is
too complicated to do that!" but that's just a lazy response and in any
case, whether it's difficult or not, you can't get a really good answer if
nobody can see the entire context in which the error happens.

Finally: I don't understand why you have a timer used to create a new
thread every so often. At the very least you should be just queuing a
work item to a thread pool thread, and you could even just create a single
thread that never exits, but instead loops with a 5-second delay at the
beginning of the loop. Doing the latter way could even change your code
enough that whatever bug you've got, it just disappears when the code is
rewritten. :)

Pete
 
Peter
Thanks for your help. I moved the line for the initializing the variable
above the address of line and everything is working fine now. I guess
working late into the night and staring at it made me over look the obvious
problem. Thanks for taking the time to help me out with this issue.

Peter Duniho said:
[...]
I have set up an array of threads and in the sub I increment the threads
on
every call to the sub, but on the second go through I get the error:
"Delegate to an instance method cannot have null 'this'."

I don't understand the code well enough, not being familiar enough with
VB. But I am suspicious of you using "INVIThread.StartInsert" as your
delegate method, and your assigning "New InventoryInsert.Inserter" to the
"INVIThread" identifier, whatever that is. In particular, I see a couple
of possibilities:

1) "INVIThread" is an actual class name. If so, the error message
implies to me that INVIThread.StartInsert is an instance method, in which
case you should be creating an instance of INVIThread and using the
reference to that instance for the operand of the "AddressOf". In this
case, I don't know what assigning some new value to the class name itself
would do in VB, but I doubt it's good.

2) "INVIThread" is the name of a variable, assigned to some class
named something else. In which case I don't understand why you would
initialize the variable on the line _after_ the line where it needs a
valid reference.

In either case, I suspect the line after the line throwing the error is
somehow related to the problem. What that might be, I don't know. I've
found that VB's design that allows the programmer to treat a classname the
same as a reference to that class in certain situations just obfuscates
buggy code and makes it harder to figure out problems. This could well be
yet another example of that.

In any case, I suspect that you might get better help posting your
question to a VB-specific newsgroup.

I also would recommend you creating a concise-but-complete sample of code
that reliably reproduces the problem. Most people complain "my code is
too complicated to do that!" but that's just a lazy response and in any
case, whether it's difficult or not, you can't get a really good answer if
nobody can see the entire context in which the error happens.

Finally: I don't understand why you have a timer used to create a new
thread every so often. At the very least you should be just queuing a
work item to a thread pool thread, and you could even just create a single
thread that never exits, but instead loops with a 5-second delay at the
beginning of the loop. Doing the latter way could even change your code
enough that whatever bug you've got, it just disappears when the code is
rewritten. :)

Pete
 
Back
Top