0

The following method is fired when a CD is inserted or ejected.

The problematic code is noted below.

Everything here is based on stepping through the code with breakpoints.

 public void CDREventArrived(object sender, EventArrivedEventArgs e) { // Get the Event object and display it PropertyData pd = e.NewEvent.Properties["TargetInstance"]; if (pd != null) { ManagementBaseObject mbo = pd.Value as ManagementBaseObject; if (mbo.Properties["VolumeName"].Value != null) { //This line is executed. textBox1.Text = "test"; //This line is not executed. //While stepping through the execution, //I hit F11 on the line above, //hoping to get to this next line, but it never happens. //The form pops back up and the step through is complete. label1.Text = "test"; } else { //Same problem here. Only the first line is hit. textBox1.Text = "test"; label1.Text = "test"; } } } 

A related problem is that while it does hit the code to change the text property of textBox1, the text is not actually changed. I can see this on the form and while stepping through breakpoints. So it appears it hits the code, but never actually executes. I'm wondering why it doesn't continue to all the code in the given conditional.

Here is the code which sets up the delegate. This method is called on FormLoad. I stop the listener on form exit.

private void CreateCDRomListener() { WqlEventQuery q; ManagementOperationObserver observer = new ManagementOperationObserver(); // Bind to local machine ConnectionOptions opt = new ConnectionOptions(); opt.EnablePrivileges = true; //sets required privilege ManagementScope scope = new ManagementScope("root\\CIMV2", opt); try { q = new WqlEventQuery(); q.EventClassName = "__InstanceModificationEvent"; q.WithinInterval = new TimeSpan(0, 0, 1); // DriveType - 5: CDROM q.Condition = @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5"; w = new ManagementEventWatcher(scope, q); // register async. event handler w.EventArrived += CDREventArrived; w.Start(); // Do something usefull,block thread for testing Console.ReadLine(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { } } 

3 Answers 3

4

My first guess is this callback occurs on a thread that is not the UI thread. Try this:

if (InvokeRequired) { Invoke(() => textBox1.Text = "test"); } else { textBox1.Text = "test"; } 
Sign up to request clarification or add additional context in comments.

1 Comment

It looks like this was the winner. Thank you. [System.InvalidOperationException] = {"Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on."}
2

I think an exception is being triggered at some point.
I would probably put a try/catch in that code to see if something is triggering an exception.

2 Comments

Thank you. This helped me find the answer. I wonder why the exception wasn't shown while debugging?
First you didn't have the proper catch on the problematic method, and probably somebody else outside your method was catching the exception and ignoring it.
0

Is there some event handler for the textbox that's being fired when you attempt to change its Text property that is dumping out early?

1 Comment

No, there is nothing associated with that textbox. I just dropped the textbox onto the form for purposes of debugging, because my initial code wasn't working. I wanted to break it down as simply as possible to see why the value wasn't getting changed.