2

I haven't really played with asyn operations before or multiple threads so this is all new to me. So I was hoping for some guidance

Suppose I have a class something like below

public class pinger
{


    // Constructor
    public Pinger()
    {
     do while exit = False;
    Uri url = new Uri("www.abhisheksur.com");
        string pingurl = string.Format("{0}", url.Host);
        string host = pingurl;
        bool result = false;
        Ping p = new Ping();
        try
        {
            PingReply reply = p.Send(host, 3000);
            if (reply.Status == IPStatus.Success)
                result = true;
        }
        catch { }
      //wait 2 seconds
      loop;
    }


}

so I can call this with

Pinger firstone = new Pinger

What I want if for control to then return to the main thread leaving the created instance running and pinging the host every two seconds and updating the result variable, that I can then use a get property when I want to know the status from the main thread.

Can any one suggest some good reading / examples to introduce me to multi threading in c#, using Ping as an example seemed a good easy thing to try this out with :)

Cheers

Aaron

3 Answers 3

3

I can outline how the class should look :-)

public class pinger
{
    private Uri m_theUri;
    private Thread m_pingThread;
    private ManualResetEvent m_pingThreadShouldStop;

    private volatile bool m_lastPingResult = false;

    public Pinger(Uri theUri)
    {
        m_theUri = theUri;
    }


    public void Start()
    {
        if (m_pingThread == null)
        {
            m_pingThreadShouldStop = new ManualResetEvent(false);
            m_pingThread = new Thread(new ParameterizedThreadStart(DoPing));
            m_pingThread.Start(m_theUri);
        }
    }

    public void Stop()
    {
        if (m_pingThread !=  null)
        {
            m_pingThreadShouldStop.Set();
            m_pingThread.Join();

            m_pingThreadShouldStop.Close();
        }
    }


    public void DoPing(object state)
    {
        Uri myUri = state as Uri;
        while (!m_pingThreadShouldStop.WaitOne(50))
        {
            // Get the result for the ping
            ...

            // Set the property
            m_lastPingResult = pingResult;
        }
    }


    public bool LastPingResult
    {
        get { return m_lastPingResult; }
    }
}

What does it do? It is a new class with a Start and a Stop method. Start starts the ping, Stop stops the ping.

Pinging is done in a separate thread and with every ping, the result property is updated.

Sign up to request clarification or add additional context in comments.

5 Comments

How the hell you guys answer so quickly!! I will have a look at this in a bit and let you know :) but this looks very promising. I can understand the logic of programming, just wish I could learn and remember the syntax like you guys do!
I'm not sure whether the syntax is 100% correct - you'll see when you paste this into Visual Studio :-) The longer you do stuff like that, the better you will remember how to do things.
ManualResetEvent, ParameterizedThreadStart, Thread, I get "using directive or an assembly reference?" I have using System.Threading.Tasks; included but is there some thing else I am missing?
Oh hold on it seem to be find if I add in (using System.Threading;)
after one minor syntax issue ;) its working great this :) thank you for a very complete and easy to follow example.
3

I would reccomend the Task Parallel Library (TPL) for this. A great article on using the TPL can be found here.

For other infomation on threading in C# can be found on Joseph Albahari's blog. This should provide all the information you need to get you started.

I hope this helps.

Edit: If you want an example of how to thread the above I will be happy to help.

1 Comment

I will look them up chees, I would say yes to the offer of an example, but I think Thorsten's below has that wrapped up. :)
0

I came up with a Task based approach where there are 3 code paths interacting - please note that most likely the work will be done with just one thread.

The output of the program shown further below is:

enter image description here

public class Program
{
    public static object _o = new object();
    public static void Main(string[] args)
    {
        PingReply pingResult = null;
        int i = 0;
        Task.Run(async () =>
        {
            var ii = 0;
            //no error handling, example only
            //no cancelling, example only 
            var ping = new System.Net.NetworkInformation.Ping();
            while (true)
            {
                Console.WriteLine($"A: {ii} > {DateTime.Now.TimeOfDay}");
                var localPingResult = await ping.SendPingAsync("duckduckgo.com");
                Console.WriteLine($"A: {ii} < {DateTime.Now.TimeOfDay}, status: {localPingResult?.Status}");
                lock (_o)
                {
                    i = ii;
                    pingResult = localPingResult;
                }

                await Task.Delay(1000);
                ii++;
            }
        });

        Task.Run(async () =>
        {
            //no error handling, example only 
            while (true)
            {
                await Task.Delay(2000);
                lock (_o)
                {
                    Console.WriteLine($"B: Checking at {DateTime.Now.TimeOfDay}, status no {i}: {pingResult?.Status}");
                }
            }
        });

        Console.WriteLine("This is the end of Main()");
        Console.ReadLine();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.