0

This one has me a bit perplexed. I have a Shown event for a form, but the event's implementation doesn't process unless I display a MessageBox.

Here is my code which does not work:

 private void MainForm_Shown(object sender, EventArgs e) { string[] arguments = Environment.GetCommandLineArgs(); foreach (var argument in arguments) { if (argument == "someArgs") { var something = doSomeStuff(); } } } 

Whenever I add a message box like this though, it works:

 private void MainForm_Shown(object sender, EventArgs e) { string[] arguments = Environment.GetCommandLineArgs(); foreach (var argument in arguments) { if (argument == "someArgs") { var something = doSomeSuff(); } else { MessageBox.Show(argument); } } } 

I have tried using both a Form_Load and Form_Activated event, but they both cause my program to crash entirely do to the implementation in doSomeStuff.

Just a little background of how this program works. I'm trying to establish a WCF connection. I have already successfully done this with some hard-coded values, but I'm trying to make it dynamic via the doSomeStuff method. I believe a race condition is occurring which the MessageBox is fixing somehow. Application A is starting Application B (the source code I have shown here) then sending a command line argument with some parameters into Application B. Without the MessageBox, Application A throws a TCP 10061 error.

Another point of interest, is there any easy way to debug WCF connections? I have access to the source of both applications, but I'm not sure if it is possible to have Application A launch Application B with the debugger (instead of just the executable). This would help immensely.

EDIT: Was playing around a bit more... found that if I use an Application.DoEvents() instead of a messageBox, it's the same result.

5
  • Is app B closing immediately. It sounds like the MessageBox.Show is what is keeping the app alive. If it's winforms, perhaps you aren't running the message loop? See stackoverflow.com/questions/31948996/…. Also, you can launch a debugger from within app B via Debugger.Launch() msdn.microsoft.com/en-us/library/… Commented Aug 11, 2015 at 19:13
  • Nah app B is fine if there is no MessageBox, it's just App A thows that TCP 10061 error. Commented Aug 11, 2015 at 19:16
  • You could put anything in the else block, there's no magic with MessageBox nor Application. Your problem is simply that the if condition never evaluates true. Use Equals() with the proper string comparison option instead of == and inspect what is coming in by step debugging, you may need to Trim() as well. Commented Aug 11, 2015 at 21:06
  • If the if never evaluates to true, how does doSomeStuff get executed then in either case? Also, I tried an empty else condition and no go. I'm almost certain now the problem is with the GUI not updating in time which is why MessageBox (which calls on Application.DoEvents implicitly) and Application.DoEvents itself seemingly fixes the issue. Commented Aug 12, 2015 at 11:52
  • And another note: if I call Application.DoEvents outside of the loop and if altogether, it fixes the issue as well. Commented Aug 12, 2015 at 11:54

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.