Process.Start not working within if statement

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1320 Views - Last Post: 30 January 2013 - 09:49 AM Rate Topic: -----

#1 scotandz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 04-May 10

Process.Start not working within if statement

Posted 28 January 2013 - 03:17 PM

Hi guys I have an if statement that doesn't seem to work when i put a point break and went through step by step the code works but if I just run it only the first process.start is run
if (Copy1 != "")
     {
      Process.Start(Copy1);
      }
      if (Copy2 != "")
      {
      Process.Start(Copy2);
      }
      if (Copy3 != "")
      {
       Process.Start(Copy3);
      }


Is This A Good Question/Topic? 0
  • +

Replies To: Process.Start not working within if statement

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,698
  • Joined: 05-May 12

Re: Process.Start not working within if statement

Posted 28 January 2013 - 03:22 PM

What are the values of Copy1, Copy2, and Copy3 as you are stepping through the code?
Was This Post Helpful? 0
  • +
  • -

#3 scotandz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 04-May 10

Re: Process.Start not working within if statement

Posted 28 January 2013 - 03:33 PM

View PostSkydiver, on 28 January 2013 - 03:22 PM, said:

What are the values of Copy1, Copy2, and Copy3 as you are stepping through the code?

Basicly its 3 links to web sites so www.google.com,www.bing.com,www.sky.com I tried it without the if statement so just
Process.Start(Copy1) 
Process.Start(Copy2)
Process.Start(Copy3)


and it worked but as I get the value for each from a text box an error may happen if nothing is entered

while stepping through the program each of the copy's had the correct value assigned and were opened in IE.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,659
  • Joined: 02-June 10

Re: Process.Start not working within if statement

Posted 28 January 2013 - 03:36 PM

First - don't compare to "" like this is 1980's BASIC

if (!String.IsNullOrWhiteSpace(Copy1)) Process.Start(Copy1);
if (!String.IsNullOrWhiteSpace(Copy2)) Process.Start(Copy2);
if (!String.IsNullOrWhiteSpace(Copy3)) Process.Start(Copy3);


Next - As Skydiver pointed out what are the runtime values of these variables? IE: Put a breakpoint at the first one and look.



tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Jan 2013
Spoiler



Was This Post Helpful? 0
  • +
  • -

#5 scotandz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 04-May 10

Re: Process.Start not working within if statement

Posted 28 January 2013 - 03:44 PM

Hi first off thanks for all the useful info.

I did add break points and when running through the code it went through each if statement as none were = "" or nothing the value of each of the copy's so copy1,copy2,copy3 were as expected they each held the value of the link I had entered in the text boxes and when running through the program debugging it actually worked the problem is when I remove the break points and run the program only copy2 is executed.

This post has been edited by tlhIn`toq: 28 January 2013 - 07:30 PM
Reason for edit:: Don't quote the entire immediately previous message: We all see it

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,698
  • Joined: 05-May 12

Re: Process.Start not working within if statement

Posted 28 January 2013 - 06:46 PM

That would indicate that either Copy1 and Copy3 were null or empty, or there was a problem trying to start what ever is associate with the contents of Copy1 and Copy3.

As an aside, running "foo.com" via Start.Process() is dangerous. What if instead of "foo.com", it was "format.com" or "command.com"? Remember that on your hard drive are potentially various programs with ".com" extensions and Start.Process() will call the Win32 API ShellExecute() which will prefer to run a local ".com" program over trying to find a matching website.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,659
  • Joined: 02-June 10

Re: Process.Start not working within if statement

Posted 28 January 2013 - 07:34 PM

Using the textboxes to be your variables is not recommended and I think this is an example as to why. If the GUI doesn't get a chance to update, then referencing those controls gets you null.

I would strongly suggest you stop looking at textboxblahblahblah.Text and start using properties. Uncouple this tight binding between code and GUI.
Was This Post Helpful? 0
  • +
  • -

#8 scotandz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 04-May 10

Re: Process.Start not working within if statement

Posted 29 January 2013 - 05:19 AM

View PostSkydiver, on 28 January 2013 - 06:46 PM, said:

That would indicate that either Copy1 and Copy3 were null or empty, or there was a problem trying to start what ever is associate with the contents of Copy1 and Copy3.

As an aside, running "foo.com" via Start.Process() is dangerous. What if instead of "foo.com", it was "format.com" or "command.com"? Remember that on your hard drive are potentially various programs with ".com" extensions and Start.Process() will call the Win32 API ShellExecute() which will prefer to run a local ".com" program over trying to find a matching website.



Hi in terms of the 1st and 3rd copy being null it doesnt seem to be the case as when i step throught the program they each have the correct value assigned and even execute only when i dont debugg the program do they not its also weird that when i comment out the 2nd and 3rd copy the first executes.

In terms of using Start.Process() i am going to use this small program to launch multiple hyperlinks i have collected so in all probibility there wont be just a simple .com will be more like http://www.dreaminco...32&qpid=1792540 although i do understand your conserns just thought the Start.Process() would be easyest to use in the this instance
Was This Post Helpful? 0
  • +
  • -

#9 scotandz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 04-May 10

Re: Process.Start not working within if statement

Posted 29 January 2013 - 05:36 AM

View PosttlhIn`toq, on 28 January 2013 - 07:34 PM, said:

Using the textboxes to be your variables is not recommended and I think this is an example as to why. If the GUI doesn't get a chance to update, then referencing those controls gets you null.

I would strongly suggest you stop looking at textboxblahblahblah.Text and start using properties. Uncouple this tight binding between code and GUI.


Hi i have changed it so now i have it saving to a db and reading from it into a list which asigns its values to each of the varibles so Copy1 has the value of Copy1 in the db but again while debgging the values are getting asigined and the start.process is executing each of them but if i simply run the program only one is executed its almost as if start.process is exiting before the other if staements are executed but while debiggin this isnt the case i also thought it might be something to do with how fast each of the start.process is called maby if it was to close together then the second was called before the first finished and that would be the problem but i added a time laps in each if statement but it didnt work just made my program slower lol.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,698
  • Joined: 05-May 12

Re: Process.Start not working within if statement

Posted 29 January 2013 - 06:10 AM

I believe that you are close to the reason, but not quite there. I think that it' not Start.Process() that is taking time, but it's firing up and communicating to your browser that is taking time. I believe that when use only a URL, the underlying ShellExecute() will use DDE to communicate with the browser instead of simply passing the URL on the command line.

Instead of:
Process.Start(url);



Try using this instead:
Process.Start("IExplore.exe", url);



Replace "IExplore.exe", with your browser. Put in a full path if needed.
Was This Post Helpful? 1
  • +
  • -

#11 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,673
  • Joined: 24-August 11

Re: Process.Start not working within if statement

Posted 29 January 2013 - 06:50 AM

Not to hijack this thread, but what is (are) the benefit(s) to comparing against String.IsNullOrWhiteSpace vs String.Empty?
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3223
  • View blog
  • Posts: 10,814
  • Joined: 12-December 12

Re: Process.Start not working within if statement

Posted 29 January 2013 - 07:15 AM

There is a discussion on SO here.

I would use IsNullOrEmpty against controls' .Text property and String.Empty against a string variable - which cannot be null unless declared as:

Dim sSome As String?    'nullable VB
string? sSome; // C#


Allowing null is really intended for use when retrieving or comparing database-data.

IsNullOrWhiteSpace I would use against a controls' Text if I want to treat just spaces (and other whitespace characters) as though the control didn't contain anything.

This post has been edited by andrewsw: 29 January 2013 - 07:19 AM

Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,698
  • Joined: 05-May 12

Re: Process.Start not working within if statement

Posted 29 January 2013 - 07:50 AM

Actually, these will fail to compile:
string? nullString;
Nullable<string> nullStringAlso;



On the other hand, this is perfectly legal:
string nullString = null;



So you would still need to check for null, and hence the existence of String.IsNullOrEmpty().
Was This Post Helpful? 1
  • +
  • -

#14 scotandz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 04-May 10

Re: Process.Start not working within if statement

Posted 29 January 2013 - 01:12 PM

View PostSkydiver, on 29 January 2013 - 06:10 AM, said:

I believe that you are close to the reason, but not quite there. I think that it' not Start.Process() that is taking time, but it's firing up and communicating to your browser that is taking time. I believe that when use only a URL, the underlying ShellExecute() will use DDE to communicate with the browser instead of simply passing the URL on the command line.

Instead of:
Process.Start(url);



Try using this instead:
Process.Start("IExplore.exe", url);



Replace "IExplore.exe", with your browser. Put in a full path if needed.


tried your suggestion and it worked first time thanks for taking the time to help.
Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3223
  • View blog
  • Posts: 10,814
  • Joined: 12-December 12

Re: Process.Start not working within if statement

Posted 29 January 2013 - 02:31 PM

View PostSkydiver, on 29 January 2013 - 07:50 AM, said:

Actually, these will fail to compile:
string? nullString;
Nullable<string> nullStringAlso;



On the other hand, this is perfectly legal:
string nullString = null;



So you would still need to check for null, and hence the existence of String.IsNullOrEmpty().


@SkyDiver Thank you; my mistake. String is a reference type and nullable by default. We can do:

int? nullInteger;
Nullable<int> nullIntegerAlso;

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2