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
  1. Home
  2. General Programming
  3. C#
  4. Threads and BeginReceive

Threads and BeginReceive

Scheduled Pinned Locked Moved C#
csharphelpdesignsysadmin
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Echilon
    wrote on last edited by
    #1

    I'm coding an FTP client in C#, and I'm trying to use System.Net.Socket's BeginReceive method to stop the UI from freezing when receiving data. At present, it works as it should, but whenever there's a transfer in progress (upload/download or directory listing), the UI freezes until it's complete. I want to use BeginReceive to recive data in a different thread. This is what I have to far: Two classes - FTP and FrmMain. In FTP I have a delegate: public delegate void DirListDelegate(object sender, ArrayList items); public event DirListDelegate OnDirListComplete; The I have three methods: private void OnDataReceived(IAsyncResult asyn) { int bytes = dataSocket.EndReceive(asyn); bldBuffer.Append(Encoding.ASCII.GetString(buffer, 0, bytes)); if(bytes < buffer.Length) { dataSocket.Close(); // Trimmed code to process data into "itemsList" OnDirListComplete(this, itemsList); } else { WaitForData(); } } private void WaitForData() { dataSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), null); } public void ListDir() { dataSocket = openSocket(); //trimmed code to make server initiate transfer ("LIST -al") bldBuffer.Remove(0, bldBuffer.Length); WaitForData(); } Then I call FTP.ListDir() from FrmMain. What should happen is the main program should be free to respond to user clicks etc and not freeze, but it's as though I'm just using Receive instead of BeginReceive. I'm really stuck on this and I'd appreciate any help. :)

    L 1 Reply Last reply
    0
    • E Echilon

      I'm coding an FTP client in C#, and I'm trying to use System.Net.Socket's BeginReceive method to stop the UI from freezing when receiving data. At present, it works as it should, but whenever there's a transfer in progress (upload/download or directory listing), the UI freezes until it's complete. I want to use BeginReceive to recive data in a different thread. This is what I have to far: Two classes - FTP and FrmMain. In FTP I have a delegate: public delegate void DirListDelegate(object sender, ArrayList items); public event DirListDelegate OnDirListComplete; The I have three methods: private void OnDataReceived(IAsyncResult asyn) { int bytes = dataSocket.EndReceive(asyn); bldBuffer.Append(Encoding.ASCII.GetString(buffer, 0, bytes)); if(bytes < buffer.Length) { dataSocket.Close(); // Trimmed code to process data into "itemsList" OnDirListComplete(this, itemsList); } else { WaitForData(); } } private void WaitForData() { dataSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(OnDataReceived), null); } public void ListDir() { dataSocket = openSocket(); //trimmed code to make server initiate transfer ("LIST -al") bldBuffer.Remove(0, bldBuffer.Length); WaitForData(); } Then I call FTP.ListDir() from FrmMain. What should happen is the main program should be free to respond to user clicks etc and not freeze, but it's as though I'm just using Receive instead of BeginReceive. I'm really stuck on this and I'd appreciate any help. :)

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Echilon wrote:

      I'm really stuck on this and I'd appreciate any help.

      Hello Echilon and welcome to code project. You posted this problem yesterday and I replied to you. Now you start a new thread of the same subject without ever responding to my reply. That is considered rude and an abuse of the CodeProject resources. Take a moment to read these posting guidelines[^] along with the first post in this forum titled "How to get an answer to your question".

      led mike

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

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