Activity Stream
48,167 MEMBERS
6854 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Page 1 of 3 123 LastLast
Results 1 to 10 of 24
  1.     
    #1
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com

    Default [C#] AutoResetEvent Freezing application!

    PHP Code: 
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.Net.NetworkInformation;
    using System.Threading;

    namespace 
    SimplePinger
    {
        public 
    partial class Form1 Form
        
    {
            
    #region Variables

            
    int PingAmount 5;
            
    AutoResetEvent Waiter = new AutoResetEvent(false);
            
    Ping pingSender = new Ping();
            
    PingOptions options = new PingOptions(64true);
            
    #endregion

            
    public Form1()
            {
                
    InitializeComponent();
                
    updatePingValue(5); //Set defualt to 5
            
    }

            private 
    void PingTimes_Scroll(object senderEventArgs e)
            {
                
    updatePingValue(PingTimes.Value);
            }

            private 
    void updatePingValue(int Value)
            {
                
    PingAmount PingTimes.Value;
                
    PingAmountLabel.Text "Pings to send: " PingAmount;
            }

            private 
    void startPing_Click(object senderEventArgs e)
            {
                
    DisableFormElements();
                
    outputBox.Clear();

                
    options.DontFragment true;

                
    pingSender.PingCompleted += new PingCompletedEventHandler(PingCallback);

                
    //Create 32 byts to send
                
    byte[] buffer Encoding.ASCII.GetBytes("                               ");
                
    int i;
                for (
    0PingAmounti++)
                {


                    
    //Here i want to run this 5 times but without the app freezing :(


                    
    pingSender.SendAsync(addressBox.Text120bufferoptions);


                }
                
    Waiter.WaitOne();
                
    EnableFormElements();
            }

            public 
    void PingCallback(object senderPingCompletedEventArgs e)
            {
                if (
    e.Cancelled)
                {
                    
    EnableFormElements();
                }

                if (
    e.Error != null)
                {
                    
    MessageBox.Show("Ping Failed:" e.Error.ToString());

                    
    // Let the main thread resume. 
                    
    ((AutoResetEvent)e.UserState).Set();
                }

                
    PingReply reply e.Reply;
                
    Waiter.Set();
                
    AddCallbackToForm(reply);
            }

            public 
    void AddCallbackToForm(PingReply Reply)
            {
                if (
    Reply == null)
                {
                    return;
                }
                if (
    Reply.Status == IPStatus.Success)
                {
                    
    outputBox.Text += "Success: " Reply.Address " - " Reply.RoundtripTime "ms";
                }
                else 
                {
                    
    outputBox.Text += "Failed...";
                }

            }

            public 
    void EnableFormElements()
            {
                
    startPing.Enabled true;
                
    PingTimes.Enabled true;
            }

            public 
    void DisableFormElements()
            {
                
    startPing.Enabled false;
                
    PingTimes.Enabled false;
            }
        }

    here is the Form Class, seems to be freezing / hanging for a while and im sure its something Very stupid but every tutorial that i read shows me that im doing it right

    Hope you can spot my error
    litewarez Reviewed by litewarez on . [C#] AutoResetEvent Freezing application! using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.NetworkInformation; Rating: 5
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  2.   Sponsored Links

  3.     
    #2
    Banned
    Website's:
    orangevps.com
    Hello,

    You should add some mutex locks in there, since it looks like there is some threading try adding some mutex locks and debug_log messages to check for the errros..

    Best of luck...

  4.     
    #3
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    thanks for the advice but i don't think that's the case.

    If i remove the Waiter, the applications does not freeze but just throws an error to say that i cant run 2 pings at the same time. So i need the waiter.

    If you can show me how to multiple thread it i will give it a go !

    ive tried the following just:

    PHP Code: 
                for (0<= PingAmounti++)
                {
                    
    Action Action = new Action(() => pingSender.SendAsync(addressBox.Text120bufferoptions));
                    new 
    Thread(new ThreadStart(Action));
                } 
    And its stopped hangineg but now the textarea is not being updated
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  5.     
    #4
    Banned
    Website's:
    orangevps.com
    Once you Multi threaded it you need to follow a guide for mutex locks otherwise your going to get a race condition..

  6.     
    #5
    Member
    Website's:
    litewarez.net litewarez.com triniwarez.com
    can you give me an example. and check my last post i edited it
    Join Litewarez.net today and become apart of the community.
    Unique | Clean | Advanced (All with you in mind)
    Downloads | Webmasters


    Notifications,Forum,Chat,Community all at Litewarez Webmasters


  7.     
    #6
    Banned
    Website's:
    orangevps.com

  8.     
    #7
    Respected Developer
    pingSender.SendAsync() shouldn't be executed from a new thread since SendAsync will not block the calling thread. It puts it on the thread pool automatically.

    Also for a 32 byte buffer:
    byte[] buffer = new byte[32];

    Finally, get rid of AutoResetEvent. I have never used it, I don't see why you should need it for a simple async ping request.

    @Angeix: locks aren't needed here.

  9.     
    #8
    Banned
    Website's:
    orangevps.com
    Am no good with C#

    Learning vb.net myself mainly good with C++ with past experience with game server emulators

  10.     
    #9
    Respected Developer
    From C++ to VB? Why not C# then? VB is the ugly duck of the programming world .

  11.     
    #10
    Banned
    Website's:
    orangevps.com
    I love vb.net works quite well.

Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Application Error Help
    By makfun in forum Technical Help Desk Support
    Replies: 1
    Last Post: 8th Apr 2012, 06:50 PM
  2. Filesonic but which application
    By erikadurane in forum File Host Discussion
    Replies: 4
    Last Post: 1st Jan 2012, 04:43 AM
  3. Replies: 109
    Last Post: 31st Oct 2011, 06:42 PM
  4. RDA Freezing?
    By EL-Warez in forum Server Management
    Replies: 1
    Last Post: 23rd Sep 2011, 01:40 AM
  5. Application Submission on ddl???????
    By power07 in forum Technical Help Desk Support
    Replies: 0
    Last Post: 1st Jan 2011, 01:02 PM

Tags for this Thread

BE SOCIAL