threading question about focus

E

Eric Sabine

OK, I have an app that when it loads, it calls a sub which loads a dataset
from a web service. I spin the call on another thread so the user doesn't
get an unresponsive screen. The problem ensues when the web service can't
be contacted (which is OK). I throw up a messagebox to the user and an
opendialog control, but since this is happening on another thread, the user
can still do activity in the main form.

I suppose this breaks a tenent of why you're doing threading in the first
place, but can someone offer a suggestion anyway?

Code goes like this
' Spin the dataset filling to a new thread
Dim ts As New ThreadStart(AddressOf FillDataSetFromWebService)
Dim t As New Thread(ts)
t.Name = "FillDataSetFromWebService"
t.Start()

thanks,
Eric
 
A

Armin Zingler

Eric Sabine said:
OK, I have an app that when it loads, it calls a sub which loads a
dataset from a web service. I spin the call on another thread so the
user doesn't get an unresponsive screen. The problem ensues when the
web service can't be contacted (which is OK). I throw up a
messagebox to the user and an opendialog control, but since this is
happening on another thread, the user can still do activity in the
main form.

I suppose this breaks a tenent of why you're doing threading in the
first place, but can someone offer a suggestion anyway?

Code goes like this
' Spin the dataset filling to a new thread
Dim ts As New ThreadStart(AddressOf FillDataSetFromWebService)
Dim t As New Thread(ts)
t.Name = "FillDataSetFromWebService"
t.Start()

If you want the message to appear in the main UI thread, the call must be
marshalled by calling the main form's Invoke method. See the method's
description for more information. Also:

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB/vbcn7/html/vaconFreeThreadingWithF
ormsControls.htm

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB/cpguide/html/cpcondevelopingmultit
hreadedwindowsformscontrol.htm

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB/dnforms/html/winforms08162002.htm

(see hints in signature)



--
Armin

- Links might be split into two lines. Concatenate them using notepad.
- Links might require to add a ".nnnn" after the "2003FEB", e.g.
"2003FEB.1033" for localized versions.
- Links starting with "ms-help" are URLs for the document explorer (<F1>).
Paste them in the URL textbox and press enter. Using internal help (menu
tools -> options -> environment -> help), display the "Web" toolbar that
contains the textbox.
 
E

Eric Sabine

Thanks Armin & CJ. Perhaps its obvious that this is new to me. Those links
will help, also I'm going to look for some whitepapers/tech articles on the
subject.

Eric
 
E

Eric Sabine

Armin or CJ,
Can you give me a little more help please?
I admit that I am a bit lost and learning by example helps greatly. The
thread example of my original post is probably the easiest method.
Where I'm struggling is on using .Invoke or .BeginInvoke, likewise am I
creating a delegate sub? This line was in the help files and I'm not sure
what it's doing
Dim r As IAsyncResult = BeginInvoke(_fileListDelegate, New Object() {files,
startingIndex, count})

Anyway, if either of you could give me a more definitive "nudge" toward the
proper direction, I would appreciate it. Have you seen any good primers on
threading too?

Thanks,
Eric
 

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

Similar Threads


Top