Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
A

Allan Eagle

@Allan Eagle
About
Posts
5
Topics
0
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Web service instance
    A Allan Eagle

    Can you post the code for the web service class? Maybe something is wrong in there?

    C# csharp wcf

  • TreeView Question (Folder Browser)
    A Allan Eagle

    Robert, Snap. Your way of dynamically creating the tree of course would be better ... Allan

    C# tutorial question

  • TreeView Question (Folder Browser)
    A Allan Eagle

    Looks like you need a bit of recursion:- private void Form1_Load(object sender, System.EventArgs e) { AddDirectories(_Directory.Nodes, new DirectoryInfo("c:\\"), 1); } private void AddDirectories(TreeNodeCollection tn, DirectoryInfo dir, int level) { if (level <= 5) { try { DirectoryInfo[] dirs = dir.GetDirectories(); foreach(DirectoryInfo subDir in dirs) { TreeNode newNode = tn.Add(x.ToString()); newNode.Tag = subDir; AddDirectories(newNode.Nodes, subDir, level + 1); } } catch { // You may want to do something here if an error occurs } } } The AddDirectories() method calls itself passing in DirectoryInfo and the new node for each new directory it finds and so on. The level variable in this example serves as a way to restrict how far into the directory tree the method should go. Also I usually store the DirectoryInfo into the tag of the newly created node so I can make use of it later when the node is selected by the user.

    C# tutorial question

  • Multiple Instances
    A Allan Eagle

    You mentioned that your previous attempt didn't work. Were you using a mutex? string uniqueid = Application.ExecutablePath.Replace(@"\", @"_"); Mutex m = new Mutex(false, uniqueid); if (m.WaitOne(1, true)) { // Application is not running so carrying on running } else { // Application is already running so end here }

    C# question

  • Form and Network Listener
    A Allan Eagle

    Yes you are correct. The code is halting within Form1.Run() (called from the constructor) so execution never reaches Application.Run(new Form1()), which is the reason you never see the form open. To acheive the result you require can be acheived by running the Form1.Run() method as a seperate worker thread which can be achieved something like:-

    public class Form1 : System.Windows.Forms.Form
    {
      private Thread trdTcpActivityListener = null;
    
      public Form1()
      {
         InitializeComponent();
    
         // Initialize seperate thread and start it
         trdTcpActivityListener = new Thread(new ThreadStart(this.Run));
         trdTcpActivityListener.Start();      
          
         // Instead of - this.Run();
      }
    
      public void Run()
      {
        TcpListener tcpListener = new TcpListener(IPAddress.Any, 65000);
        tcpListener.Start();
    
        while (true)
        {
          // Use .Pending() to determine if there is any connections
          if (tcpListener.Pending())
          {
    	Socket socketForClient = tcpListener.AcceptSocket();
            socketForClient.Close();
    	// Maximize the form
    	this.WindowState = FormWindowState.Maximized;      
          }
    
          // Make sure the while loop doesn't hog the CPU
          Thread.Sleep(30);
        }
      }
    }
    

    Now whilst the worker thread waits for a TCP connection, the main thread is allowed to continue, the constructor completes and the form opens (minimized). Ill leave it up to you to work out when you should stop the worker thread.

    C# csharp help graphics sysadmin docker
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups