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

Need help with multithreading and InvalidOperationException

I've got a form element (a wizard, actually) which is giving me
problems when I try to invoke some of its methods from one of the
methods of a filesystemwatcher. Basically the watcher sits there
waiting to see the file and then it's supposed to go to the next page
(wizard1.Next()).

I get an InvalidOperationException when I do this, though, and as near
as I can tell it's because the watcher creates a new thread and it's
trying to access the form object from a different thread. I don't know
how to fix this, though. Any ideas? I found this helpful page (http://
msdn2.microsoft.com/en-us/library/ms171728.aspx) which seems to be
exactly what I'm asking about, but I just can't figure it out. I've
tried to adapt it to my situation, but it's just over my head... In
particular, the whole backgroundwatcher thing isn't working. For
example, if I do "BackgroundWatcher bgw" and then on the very next
line something like "bgw=new BackgroundWatcher()" it tells me that the
variable bgw is unknown.

Is there any way to force a FileSystemWatcher to use the same thread
it's started in? I don't want or need the user to do anything until
it's done looking anyways.

Mar 15 '07 #1
2 1453
Ian Toltz <it****@gmail.comwrote:
I've got a form element (a wizard, actually) which is giving me
problems when I try to invoke some of its methods from one of the
methods of a filesystemwatcher. Basically the watcher sits there
waiting to see the file and then it's supposed to go to the next page
(wizard1.Next()).

I get an InvalidOperationException when I do this, though, and as near
as I can tell it's because the watcher creates a new thread and it's
trying to access the form object from a different thread. I don't know
how to fix this, though. Any ideas?
See http://pobox.com/~skeet/csharp/threads/winforms.shtml

Basically, you need to use Control.Invoke/BeginInvoke.

<snip>
Is there any way to force a FileSystemWatcher to use the same thread
it's started in?
You can set the SynchronizingObject property to be one of your
controls. That way I believe the calls should end up on the UI thread.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 15 '07 #2
On Mar 15, 4:30 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Ian Toltz <ito...@gmail.comwrote:
I've got a form element (a wizard, actually) which is giving me
problems when I try to invoke some of its methods from one of the
methods of a filesystemwatcher. Basically the watcher sits there
waiting to see the file and then it's supposed to go to the next page
(wizard1.Next()).
I get an InvalidOperationException when I do this, though, and as near
as I can tell it's because the watcher creates a new thread and it's
trying to access the form object from a different thread. I don't know
how to fix this, though. Any ideas?

Seehttp://pobox.com/~skeet/csharp/threads/winforms.shtml

Basically, you need to use Control.Invoke/BeginInvoke.

<snip>
Is there any way to force a FileSystemWatcher to use the same thread
it's started in?

You can set the SynchronizingObject property to be one of your
controls. That way I believe the calls should end up on the UI thread.

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
That helped a lot and I got it working. Man, that's some weird stuff.

Anyways, thanks a ton!

Mar 16 '07 #3

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

Similar topics

2
by: Gianguz | last post by:
I'd like to discuss about the opportunity to have a global objects creator that introduces into a general framework (suited for multithreading) a controlled semantic to manage globals variables...
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.
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: WithPit | last post by:
I am trying to create an Managed C++ Wrapper around an unmanaged library which contains C++ code. Some of the unmanaged methods returns an returntype which is of the abstract base type (for...
6
by: bill salkin | last post by:
I setup a "Try..." block and attempted to open a non- existant database. It went to the second "catch" not the first. What is the proper "catch" clause for this specific case? TIA, Bill
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
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...
4
by: Chris | last post by:
I don't know if it's me or .net is just getting more complicated. I am trying to call a sp and pass a parameter and return the data using VB2005. First I couldn't find any docs with samples. Second...
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
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.