0

I'm not sure if this is possible or not, and I'm afraid I have no code at the moment, but I'm working on it. I've been searching for techniques like what I'm trying to do, but I'm not sure if anything is out there... let me explain conceptualy what I'm trying to do...

I have a small multi-threaded VB.net application I'm writing. In this application I'm using a com object. Initially I was trying to share this object between multiple threads - no dice. It was breaking on a wait for event line, which was when I learned about message pumping... Which I'm not sure I'm up to.

So, what I would like to try to do is set up a thread with that object and keep it running in a loop waiting for input from one of the other threads. When it gets such input, I'd like it to use the com object to perform a few tasks and then update a private class level collection, and then return to looping until I kill it.

I've been looking around to see if any sort of functionality like this is described anywhere, but I'm coming up dry. I'd most likely be creating this thread as a task... here's sort of what I'm thinking the first part would look like...

Dim openCheckTask As Task
openCheckTask = Task.Factory.StartNew(Sub() openOrderCheck())

...

Sub openOrderCheck()

Dim myComObject As ComObject = New ComObject

Do
    Wait for input '<- ?  This sort of plays into what I'm looking for...
    myComObject.doSomeStuff
    SomeCollection.Add(someValue)
Loop 

End Sub

I really have no idea how to proceed from there. It's also worth noting that this thread would need to have a cue of commands, it could be executing one and have another or more waiting to be executed... not sure if that is possible either.

Any hints or suggestions are appreciated! Thanks again for your time everyone.

MattB
  • 2,203
  • 5
  • 26
  • 48
  • so basically you want to put that code into a thread? – washcloth Feb 25 '14 at 18:29
  • Well, basically yes, but completely, no. I understand how to put that code into a thread, and I even understand how to pass intial arguments to it.. like `StartNew(Sub() openOrderCheck(someValue))`, but what I'd really like to do is keep that thread running and pass arguments to it over and over again, if that is even possible. I'm just trying to work around an issue with the com object. I'd like to have some event where it waits for input from another thread after the thread begins. – MattB Feb 25 '14 at 18:32
  • you could make global variables and read off that. – washcloth Feb 25 '14 at 18:33
  • put into a while loop and check when variables change when they do change run it – washcloth Feb 25 '14 at 18:34
  • @ExcelledProducts... true... It would be difficult to have the requests stack up, but I suppose I could use a collection and just add and remove items from it as I processed them... Hmmm... that might be the way to go... – MattB Feb 25 '14 at 18:35
  • @ExcelledProducts... would it maybe be better to have an event though? I've noticed that having code loop and check a value over and over again can be problematic... seems like I might run into issues with synclock... but maybe not. – MattB Feb 25 '14 at 18:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48403/discussion-between-excelledproducts-and-user2761919) – washcloth Feb 25 '14 at 18:43
  • possible duplicate of [How to post messages to an STA thread running a message pump?](http://stackoverflow.com/questions/21680738/how-to-post-messages-to-an-sta-thread-running-a-message-pump) – Hans Passant Feb 25 '14 at 21:02
  • @HansPassant Actually, running a message pump is exactly what I'm trying to avoid here. At the same time, I do understand that is one potential solution to this situation provided you are trying to share a com object between threads. I'm actually trying to avoid that by having one thread that declares and uses this particular com object to get around message pumping. Having said that, knowing how to set up a message pump may very well be useful to people asking the same kind of question. – MattB Feb 25 '14 at 23:43
  • Sure, using a COM object on only one thread is pretty essential to make it perform well. All that you have to figure out is how to get *your* code to run on that specific thread. Which is what it does. – Hans Passant Feb 26 '14 at 00:20
  • @HansPassant So... I've been doing some more research and I'm begining to come under the impression that message pumping will be necessary even if I creat, intantiate, and use the object only on one new thread. Is that correct? – MattB Feb 26 '14 at 16:59
  • It might be correct, there certainly are COM components that assume that the thread they were created on pumps. Which is a fair expectation, an STA thread is required to pump. So they'll expect PostMessage() to work. WebBrowser is a good example, it won't raise events when you create it on a thread that doesn't pump. – Hans Passant Feb 26 '14 at 17:05
  • @HansPassant Well... I think for now I'm going to see how it goes using this object on one thread... It only seems to run into issues so far if I try to pass it from thread to thread, or if I have lots of them running on seperate threads (> 50). For your own reference, and if it helps me any, it is a Reflection 14.0 AS400 Terminal Emulator from Attachmante. I don't seem to be able to determine if it is STA or MTA, and I'm still having difficulty finding a good example of how to set up a message loop, if that is ultimately what I need to do. – MattB Feb 26 '14 at 18:54

0 Answers0