PDA

View Full Version : نحوه ی اجرای رویداد تایمر در یک thread دیگر



debugger
شنبه 08 بهمن 1390, 11:36 صبح
من در داخل رویداد tick تایمر کد های زیر را نوشتم که اگر دقت کنید یک تابع extractyahooid هم در اخر کد صدا زده میشه حالا من میخوام ببینم چطوری این بخش از کد هامو در داخل یک thread اجرا کنم



private void TimerExtract_Tick(object sender, EventArgs e)
{
try
{
urlsourcecode = string.Empty;
string inputurl = "http://wwwwwww.ir/son?rnd=" + r.Next(1000, 99999999).ToString() + "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(inputurl);
//request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader urlinputreader = new StreamReader(response.GetResponseStream());
urlsourcecode = urlinputreader.ReadToEnd();
ExtractYahooId();
}
catch
{
return;
}
}

من اومدم یک تابع برای enable کردن تایمر نوشتم و بعد اون تابع را با ترد صدا زدم ولی اونطوری نه خطا میده نه کار می کنه
با تشکر از دوستان

debugger
شنبه 08 بهمن 1390, 20:51 عصر
برای حل مشکل از
System.Threading.Timer استفاده کردم ولی بعد از مدتی تابع خارج میشه . اصلا هم نمی دونم چرا . شما کد زیر را اجرا کنید بعد از 10 ثانیه برنامه خارج میشه . کی میدونه اشکال کار کجاست ؟؟؟

به کد های زیر دقت کنید


using System;

using System.Threading;

public class Test

{
static void Main()
{
Console.WriteLine("Started at {0:HH:mm:ss.fff}", DateTime.Now);
// Start in three seconds, then fire every one second

using (Timer timer = new Timer(new TimerCallback(Tick), null, 3000, 1000))
{
// Wait for 10 seconds

Thread.Sleep(10000);
// Then go slow for another 10 seconds

timer.Change(0, 2000);
Thread.Sleep(10000);
}
// The timer will now have been disposed automatically due to the using

// statement, so there won't be any other threads running, and we'll quit.

}
static void Tick(object state)
{
Console.WriteLine("Ticked at {0:HH:mm:ss.fff}", DateTime.Now);
}
}