By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,245 Members | 2,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,245 IT Pros & Developers. It's quick & easy.

return a value from other thread?

P: 92
okay so i have this situation:
i have to get round trip time (ping) from a IP, but if i do it in the UI thread it freezes if the ping is high, for example > 300, now to stop the UI to hang i made the ping in another thread, but how can i return value from the 2nd thread(the one pinging)?, i cant directly change the label because this code is in a dll, also even if i use delegates to make new thread (that support return value) the first thread wont stop for 2nd thread to give the return value, if i use thread.join then the whole idea of making ping in 2nd thread is useless because it just joines the 1st thread and freeze it...

any helps?
Sep 18 '09 #1
Share this Question
Share on Google+
4 Replies


GaryTexmo
Expert 100+
P: 1,501
I'm not 100% sure so I'll just throw this out there...

Have your ping thread call the delegate when it's done, then have the main thread, when it receives that call, update the label. The form runs normally.

Nothing should have to wait on anything, it just updates when it gets the call.

Does that make any sense? If you've tried that and it doesn't work, then sorry. I haven't done much work with delegates, but this seems like any other event driven process. The main form should update when it gets the data, but it shouldn't need to wait on it.
Sep 18 '09 #2

P: 92
as i said, the code is in a DLL, so i just dont have a label here, i dont want the form calling for ping request to freeze

and once again gary, u r the one helping me, suspicious how always u help me.. lol
Sep 19 '09 #3

GaryTexmo
Expert 100+
P: 1,501
Suspicious? Nah... maybe I just troll this forum too much ;)

Anyway, are you saying the code to the ping program is in a DLL? If so, you should still be able to create a thread to run the ping process, then call a delegate when it returns.

Can you please provide more details?
Sep 19 '09 #4

P: 92
well actually nvm it, i spent a hour while i was sleepy cuz i was up all night and wrote this code, works exactly as i want, it may look noobish or ugly cuz i wrote it at 7 AM waking all night...
Expand|Select|Wrap|Line Numbers
  1.         /// <summary>
  2.         /// Ping a web address or IP
  3.         /// </summary>
  4.         public sealed class Ping
  5.         {
  6.             #region variables & objects
  7.             Label lbl;
  8.             Form owner;
  9.             bool Continue = true;
  10.             string address = null;
  11.             #endregion
  12.  
  13.             #region constructor
  14.             /// <summary>
  15.             /// Ping a web address or IP
  16.             /// </summary>
  17.             /// <param name="Address">The Web Address or IP Address</param>
  18.             /// <param name="Caller">The Form that is calling this class</param>
  19.             public Ping(string Address, Form Caller)
  20.             {
  21.                 address = Address;
  22.                 owner = Caller;
  23.                 ThreadStart starter = delegate { pingip(); };
  24.                 Thread pinger = new Thread(starter);
  25.                 pinger.Start();
  26.             }
  27.             #endregion
  28.  
  29.             #region events
  30.  
  31.             #region events
  32.             void lbl_TextChanged(object sender, EventArgs e)
  33.             {
  34.                 string[] pingip = lbl.Text.Split('|');
  35.                 try { owner.Invoke((MethodInvoker)delegate { try { PingResult(Convert.ToInt32(pingip[0]), pingip[1]); } catch { } }); }
  36.                 catch { }
  37.             }
  38.             #endregion
  39.  
  40.             #region custom events
  41.             #region handlers
  42.             /// <summary>
  43.             /// PingResult Event Handler
  44.             /// </summary>
  45.             /// <param name="Ping">The ping result</param>
  46.             /// <param name="IP">IP being pinged</param>
  47.             public delegate void PingHandler(int Ping, string IP);
  48.             /// <summary>
  49.             /// Error Event Handler
  50.             /// </summary>
  51.             public delegate void ErrorHandler(Exception ex);
  52.             #endregion
  53.  
  54.             #region event declare
  55.             /// <summary>
  56.             /// Event raised when we get result of the ping.
  57.             /// </summary>
  58.             public event PingHandler PingResult;
  59.             /// <summary>
  60.             /// Event raised when some error occurs.
  61.             /// </summary>
  62.             public event ErrorHandler OnError;
  63.             #endregion
  64.             #endregion
  65.  
  66.             #endregion
  67.  
  68.             #region methods
  69.  
  70.             #region public
  71.             /// <summary>
  72.             /// Stop pinging the address given
  73.             /// </summary>
  74.             public void StopPing()
  75.             {
  76.                 Continue = false;
  77.             }
  78.             #endregion
  79.  
  80.             #region private
  81.             private void pingip()
  82.             {
  83.                 try
  84.                 {
  85.                     lbl = new Label();
  86.                     lbl.TextChanged += new EventHandler(lbl_TextChanged);
  87.                     while (Continue)
  88.                     {
  89.                         System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
  90.                         PingReply reply = ping.Send(address);
  91.                         try { lbl.Text = reply.RoundtripTime.ToString() + "|" + reply.Address.ToString(); }
  92.                         catch { lbl.Text = reply.RoundtripTime.ToString() + "|Error getting IP"; }
  93.                     }
  94.                     return;
  95.                 }
  96.                 catch (Exception ex) { owner.Invoke((MethodInvoker)delegate { try { OnError(ex); } catch { } }); }
  97.             }
  98.             #endregion
  99.  
  100.             #endregion
  101.         }
Sep 19 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.