473,769 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with multithreading and InvalidOperatio nException

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 filesystemwatch er. 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 InvalidOperatio nException 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 backgroundwatch er thing isn't working. For
example, if I do "BackgroundWatc her bgw" and then on the very next
line something like "bgw=new BackgroundWatch er()" it tells me that the
variable bgw is unknown.

Is there any way to force a FileSystemWatch er 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 1473
Ian Toltz <it****@gmail.c omwrote:
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 filesystemwatch er. 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 InvalidOperatio nException 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 FileSystemWatch er to use the same thread
it's started in?
You can set the SynchronizingOb ject property to be one of your
controls. That way I believe the calls should end up on the UI thread.

--
Jon Skeet - <sk***@pobox.co m>
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.com wrote:
Ian Toltz <ito...@gmail.c omwrote:
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 filesystemwatch er. 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 InvalidOperatio nException 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 FileSystemWatch er to use the same thread
it's started in?

You can set the SynchronizingOb ject 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
1559
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 (objects and scalar-types). In the following example Global is able to create objects of any kind with and index value attached to. So a Global<0, string> is a unique string instance object allocated into the system that can be accessed from any...
47
3755
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
2139
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 when I got two threads going at the same time. What I have is two text boxes (textBox1 and textBox2) and four buttons(cmdStartThread1, cmdStartThread2, cmdStopThread1, cmdStopThread2)
9
2426
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 example unmanagedObject). How can i convert this to the managed abstract basetype? Hope somebody can help me Thanx
6
4454
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
19599
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 having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
5
2488
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 multithreading online using C# .
4
1922
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 I tryed this <%@ WebService Language="VB" Class="GetList" %> Imports System.Web Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.Data Imports System.Data.SqlClient
7
16312
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 Exceptional C++, for example). Platform-specific (e.g.: Win32, POSIX) is OK, as long as it's good :) Thank you, Ray
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10038
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9987
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9857
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7404
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.