473,326 Members | 2,148 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,326 software developers and data experts.

Threading issue -- InvokeRequired on Form is unreliable.

Problem. Work-around. No issues, just looking to see
if I'm the only one that's seen this.

I just lost 3 hours (shoulda found it sooner) to the
InvokeRequired on a form being somewhat unreliable. It
will sometimes return incorrect results.

I've got it isolated to the Form's Handle property. If
nothing touches my form's Handle before the supplementary
threads are fired off, then InvokeRequired has about a
50/50 shot of being correct.

The minute something touches the Form's Handle property,
it becomes 100% reliable.

I'm guessing (relatively new to multi-threading: 1st day
at it today <grin>) that maybe there's some kind of delay
on a WinForm that a handle doesn't get created right away,
as soon as the Form is instantiated. My guess is that the
Handle is somehow delayed, and under certain circumstances,
that causes the handle to be created under one of my
supplementary (non-UI) threads. I'm also guessing that
InvokeRequired uses the handle to get back to the actual
Thread (somehow). So, it appears to me (by some serious
guessing) that if I don't touch the Handle in any way
before my Thread kicks off, but somewhere in my thread,
the Handle is first touched (and first created), then
the Handle is created on my Thread, rather than on the
UI thread.

This is a whole bunch-o-pulled out my arse wild guessing,
but it makes sense to me.

If I do anything that touches the Handle first, before
I fire my Thread(s) off, then everything works AOK and
InvokeRequired works as advertised. If I don't touch
the Handle, it goes all squirrely.

Can anyone duplicate or confirm this. Am I off base
here?

For now, the first thing I do on the Form is set a
useless variable to the Handle, so that something has
touched it and it gets created. It's solved the problem,
but I'm not sure if the above is out in left field or
if I'm on the right track on why it's happening.

I've looked at MS and couldn't find anything.

Nov 17 '05 #1
2 4832
Oops! Sorry guys. I meant to post that to the WindowsForms
group.
"Sgt. Sausage" <no****@nowhere.com> wrote in message
news:c5**************************@FUSE.NET...
Problem. Work-around. No issues, just looking to see
if I'm the only one that's seen this.

I just lost 3 hours (shoulda found it sooner) to the
InvokeRequired on a form being somewhat unreliable. It
will sometimes return incorrect results.

I've got it isolated to the Form's Handle property. If
nothing touches my form's Handle before the supplementary
threads are fired off, then InvokeRequired has about a
50/50 shot of being correct.

The minute something touches the Form's Handle property,
it becomes 100% reliable.

I'm guessing (relatively new to multi-threading: 1st day
at it today <grin>) that maybe there's some kind of delay
on a WinForm that a handle doesn't get created right away,
as soon as the Form is instantiated. My guess is that the
Handle is somehow delayed, and under certain circumstances,
that causes the handle to be created under one of my
supplementary (non-UI) threads. I'm also guessing that
InvokeRequired uses the handle to get back to the actual
Thread (somehow). So, it appears to me (by some serious
guessing) that if I don't touch the Handle in any way
before my Thread kicks off, but somewhere in my thread,
the Handle is first touched (and first created), then
the Handle is created on my Thread, rather than on the
UI thread.

This is a whole bunch-o-pulled out my arse wild guessing,
but it makes sense to me.

If I do anything that touches the Handle first, before
I fire my Thread(s) off, then everything works AOK and
InvokeRequired works as advertised. If I don't touch
the Handle, it goes all squirrely.

Can anyone duplicate or confirm this. Am I off base
here?

For now, the first thing I do on the Form is set a
useless variable to the Handle, so that something has
touched it and it gets created. It's solved the problem,
but I'm not sure if the above is out in left field or
if I'm on the right track on why it's happening.

I've looked at MS and couldn't find anything.

Nov 17 '05 #2
Just had similar problems with InvokeRequired. Debug lines indicate
that it was returning false when the code was invoked by my worker
threads, until a later point when it starts returning the right value
again.

Have you tried the form's CreateHandle() method? It seems to work for
me in ensuring that InvokeRequired returns the correct value.
Sgt. Sausage wrote:
Oops! Sorry guys. I meant to post that to the WindowsForms
group.
"Sgt. Sausage" <no****@nowhere.com> wrote in message
news:c5**************************@FUSE.NET...
Problem. Work-around. No issues, just looking to see
if I'm the only one that's seen this.

I just lost 3 hours (shoulda found it sooner) to the
InvokeRequired on a form being somewhat unreliable. It
will sometimes return incorrect results.

I've got it isolated to the Form's Handle property. If
nothing touches my form's Handle before the supplementary
threads are fired off, then InvokeRequired has about a
50/50 shot of being correct.

The minute something touches the Form's Handle property,
it becomes 100% reliable.

I'm guessing (relatively new to multi-threading: 1st day
at it today <grin>) that maybe there's some kind of delay
on a WinForm that a handle doesn't get created right away,
as soon as the Form is instantiated. My guess is that the
Handle is somehow delayed, and under certain circumstances,
that causes the handle to be created under one of my
supplementary (non-UI) threads. I'm also guessing that
InvokeRequired uses the handle to get back to the actual
Thread (somehow). So, it appears to me (by some serious
guessing) that if I don't touch the Handle in any way
before my Thread kicks off, but somewhere in my thread,
the Handle is first touched (and first created), then
the Handle is created on my Thread, rather than on the
UI thread.

This is a whole bunch-o-pulled out my arse wild guessing,
but it makes sense to me.

If I do anything that touches the Handle first, before
I fire my Thread(s) off, then everything works AOK and
InvokeRequired works as advertised. If I don't touch
the Handle, it goes all squirrely.

Can anyone duplicate or confirm this. Am I off base
here?

For now, the first thing I do on the Form is set a
useless variable to the Handle, so that something has
touched it and it gets created. It's solved the problem,
but I'm not sure if the above is out in left field or
if I'm on the right track on why it's happening.

I've looked at MS and couldn't find anything.


Nov 17 '05 #3

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

Similar topics

8
by: Z D | last post by:
Hello, I'm having a strange problem that is probably due to my lack of understanding of how threading & COM Interop works in a WinForms.NET application. Here's the situation: I have a 3rd...
3
by: Asad | last post by:
Hi, I am trying to write some threading code to my application. The reason I've been tempted to do this is because, I am doing some FTP uploads, and sometimes during the put method, the...
5
by: Robert W. | last post by:
I'm creating a WinForms app that will act as a companion (think administrator functionality) to a Pocket PC app. Generally the WinForms app works under just the UI thread. But if a Pocket PC...
0
by: Eric Sabine | last post by:
OK, I'm trying to further my understanding of threading. The code below I wrote as kind of a primer to myself and maybe a template that I could use in the future. What I tried to do was pass data...
0
by: Pawan Narula via DotNetMonster.com | last post by:
hi all, i'm using VB.NET and trying to code for contact management in a tree. all my contacts r saved in a text file and my C dll reads them one by one and sends to VB callback in a sync mode...
14
by: Christian Kaiser | last post by:
We have a component that has no window. Well, no window in managed code - it uses a DLL which itself uses a window, and this is our problem! When the garbage collector runs and removes our...
14
by: Simon Verona | last post by:
I think I'm doing this wrong : I have a class with a public shared method such as follows: public shared sub myFunction dim frm as new myFrm dim t as new Threading.Thread(Addressof ...
0
by: Flack | last post by:
Hey guys, I'm trying to read a file and add some nodes to a treee view control. I would like the GUI to remain interactive and have a progress bar and text box be updated with info as the file...
2
by: =?Utf-8?B?TWljaGFlbCBNYWVz?= | last post by:
Hello, I have an Mdi-application which also is a remoting-server (Ipc). Several instances can co-exist on the same machine. I have a small app (search-engine) (remoting-client) which calls a...
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: 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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.