-1

I am currently using Microsoft Terminal Services Client what works amazingly and does what i want it to do. But i have ran into a small issue what is proving to be difficult to fix. I am trying to connect to the rdp and then constantly scan if a file exist. But when i do the for loop (After it call the rdp to connect) it doesn't connect. Here is my code:

                axMsTscAxNotSafeForScripting1.Server = "0.0.0.0";
                axMsTscAxNotSafeForScripting1.UserName = "Test";
                IMsTscNonScriptable secured = (IMsTscNonScriptable)axMsTscAxNotSafeForScripting1.GetOcx();
                secured.ClearTextPassword = "c";
                axMsTscAxNotSafeForScripting1.Connect();
                Thread.Sleep(2000);
                for(; ; )
                {
                    if (File.Exists(b + "t.txt"))
                    {
                        MessageBox.Show("File Exists");
                    }
                }
mxmissile
  • 11,464
  • 3
  • 53
  • 79

2 Answers2

0

I don't think that the for loops affect connection. But, you are creating an infinite loop which does not terminates in any circumstance. Which means you are blocking the Executing thread.

Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
0

You shouldn't use a loop to poll for the existence of the file, that's what FileSystemWatcher is for. You'll need to do something like this.

var watcher= new FileSystemWatcher(b);
watcher.Filter = "t.txt";
watcher.Created += (sender, eventArgs) => MessageBox.Show("File Exists");

I've made some assumptions about what's in your b variable, but the point is, the watcher will watch a directory for changes, set the filter for the types of files you're looking for (it will accept wildcards).

If you want to prevent the program from exiting, don't use an infinite loop, just use Console.ReadKey or Console.ReadLine so you're not doing a busy wait.

Console.WriteLine("Press any key to exit.");
Console.ReadKey();
Doctor Jones
  • 21,196
  • 13
  • 77
  • 99