PDA

View Full Version : اجرای Win Application یا Console application به مدت طولانی !



littledemon
دوشنبه 12 فروردین 1392, 11:47 صبح
سلام
بچه ها من یه اپلیکیشن دارم مینویسم که روی یه سرور قرار میگیره و کلا باید اجرا باشه .و از اونجایی که سرور ها هم معمولا خاموش نمیشن ، در طول این زمان برنامه من باید طاقت بیاره :لبخند:

حالا یه سری سوال دارم :
1- برنامه من باید هنگام پیاده سازی ، چه مواردی داخلش تعبیه بشه که بتونه مدت زمان طولانی در حال اجرا باشه ؟ مثلا 100 روز .

2- من دارم از System.Timer استفاده میکنم تا یه سری اطلاعات رو سر زمان های مختلف بروز کنم یا کنم ، با استفاده از تایمر ها برنامه خیلی سریع تر از حالت عادی به فنا میره . یعنی بعضی مواقع قبل از 1 روز کراش می کنه ! این مشکل رو با استفاده از Thread ها هم داشتم . و فکر میکنم از GC باشه .:متفکر:

3-آیا پیاده سازی این اعمال تکراری که سر زمان های مختلف انجام میشن درسته توی Windows service پیاده سازی بشن ؟ چند تن از اهالی اونوری(خارجکی ها) میگفتن که کار اشتباهیه استفاده از سرویس . چرا ؟

hessam2003
دوشنبه 12 فروردین 1392, 11:55 صبح
سلام.
عموما بدترین و یکی از دشوار ترین برنامه نویسی ها همین برنامه نویسی بر روی سرور هاست
چیزی که به ذهن من میرسه اینه که شما باید ببینید برنامتون تا چند روز میتونه درحال اجرا باشه (که به کد و نحوه ای نوشتن انها بستگی داره)، و بعد میتونید در میانگین زمانی که بدست میارید برنامه را Refresh ویا بهتر از آن به صورت خودکار سر زمان مورد نظرتون برنامه رو ReRun کنید.
برای اینکار هم میتونید به وسیله API ها انجام بدید.

littledemon
دوشنبه 12 فروردین 1392, 12:39 عصر
خوب یه بحثی مطرحه آخه !
من داخل این اپلیکیشن دارم هر چند ثانیه یه سری چیزا رو کنترل میکنم.. برای ثانیه که دیگه نمیتونم از schedule استفاده کنم که اصلا صرف داره ؟

فرض کن دارم دائما فایل لوگ سیستم داخل IIS رو چک میکنم ! حالا این رو اگه Schedule کنم بابای سیستم در نمیاد ؟:لبخند:

plus
دوشنبه 12 فروردین 1392, 12:51 عصر
خود تایمر به نوبه خودش فشاری به سیستم نمیاره، Interval ش باید متناسب با عملی که داخلش انجام میدین باشه...در کل بعید میدونم دلیل Crash خود Timer باشه.اگه بتونید به روشی علتش رو پیدا کنید خوبه.

tooraj_azizi_1035
دوشنبه 12 فروردین 1392, 14:38 عصر
در طول این زمان برنامه من باید طاقت بیاره
چیزی به نام "طاقت آوردن" نداریم برنامه تا زمانی که سیستم روشنه و بسته نشه اجرا میشه.
ضمناً باید به شکل سرویس نوشته بشه چون کاربر با برنامه کاری نداره.


منبع:http://stackoverflow.com/questions/1498498/windows-service-that-runs-periodically
با Task و بدون Timer:

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
Task perdiodicTask = PeriodicTaskFactory.Start(() =>
{
Console.WriteLine(DateTime.Now);
}, intervalInMilliseconds: 2000, // fire every two seconds...
maxIterations: 10); // for a total of 10 iterations...

perdiodicTask.ContinueWith(_ =>
{
Console.WriteLine("Finished!");
}).Wait();
}
}

/// <summary>
/// Factory class to create a periodic Task to simulate a <see cref="System.Threading.Timer"/> using <see cref="Task">Tasks.</see>
/// </summary>
public static class PeriodicTaskFactory
{
/// <summary>
/// Starts the periodic task.
/// </summary>
/// <param name="action">The action.</param>
/// <param name="intervalInMilliseconds">The interval in milliseconds.</param>
/// <param name="delayInMilliseconds">The delay in milliseconds, i.e. how long it waits to kick off the timer.</param>
/// <param name="duration">The duration.
/// <example>If the duration is set to 10 seconds, the maximum time this task is allowed to run is 10 seconds.</example></param>
/// <param name="maxIterations">The max iterations.</param>
/// <param name="synchronous">if set to <c>true</c> executes each period in a blocking fashion and each periodic execution of the task
/// is included in the total duration of the Task.</param>
/// <param name="cancelToken">The cancel token.</param>
/// <param name="periodicTaskCreationOptions"><see cref="TaskCreationOptions"/> used to create the task for executing the <see cref="Action"/>.</param>
/// <returns>A <see cref="Task"/></returns>
/// <remarks>
/// Exceptions that occur in the <paramref name="action"/> need to be handled in the action itself. These exceptions will not be
/// bubbled up to the periodic task.
/// </remarks>
public static Task Start(Action action,
int intervalInMilliseconds = Timeout.Infinite,
int delayInMilliseconds = 0,
int duration = Timeout.Infinite,
int maxIterations = -1,
bool synchronous = false,
CancellationToken cancelToken = new CancellationToken(),
TaskCreationOptions periodicTaskCreationOptions = TaskCreationOptions.None)
{
Stopwatch stopWatch = new Stopwatch();
Action wrapperAction = () =>
{
CheckIfCancelled(cancelToken);
action();
};

Action mainAction = () =>
{
MainPeriodicTaskAction(intervalInMilliseconds, delayInMilliseconds, duration, maxIterations, cancelToken, stopWatch, synchronous, wrapperAction, periodicTaskCreationOptions);
};

return Task.Factory.StartNew(mainAction, cancelToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}

/// <summary>
/// Mains the periodic task action.
/// </summary>
/// <param name="intervalInMilliseconds">The interval in milliseconds.</param>
/// <param name="delayInMilliseconds">The delay in milliseconds.</param>
/// <param name="duration">The duration.</param>
/// <param name="maxIterations">The max iterations.</param>
/// <param name="cancelToken">The cancel token.</param>
/// <param name="stopWatch">The stop watch.</param>
/// <param name="synchronous">if set to <c>true</c> executes each period in a blocking fashion and each periodic execution of the task
/// is included in the total duration of the Task.</param>
/// <param name="wrapperAction">The wrapper action.</param>
/// <param name="periodicTaskCreationOptions"><see cref="TaskCreationOptions"/> used to create a sub task for executing the <see cref="Action"/>.</param>
private static void MainPeriodicTaskAction(int intervalInMilliseconds,
int delayInMilliseconds,
int duration,
int maxIterations,
CancellationToken cancelToken,
Stopwatch stopWatch,
bool synchronous,
Action wrapperAction,
TaskCreationOptions periodicTaskCreationOptions)
{
TaskCreationOptions subTaskCreationOptions = TaskCreationOptions.AttachedToParent | periodicTaskCreationOptions;

CheckIfCancelled(cancelToken);

if (delayInMilliseconds > 0)
{
Thread.Sleep(delayInMilliseconds);
}

if (maxIterations == 0) { return; }

int iteration = 0;

////////////////////////////////////////////////////////////////////////////
// using a ManualResetEventSlim as it is more efficient in small intervals.
// In the case where longer intervals are used, it will automatically use
// a standard WaitHandle....
// see http://msdn.microsoft.com/en-us/library/vstudio/5hbefs30(v=vs.100).aspx (http://msdn.microsoft.com/en-us/library/vstudio/5hbefs30%28v=vs.100%29.aspx)
using (ManualResetEventSlim periodResetEvent = new ManualResetEventSlim(false))
{
////////////////////////////////////////////////////////////
// Main periodic logic. Basically loop through this block
// executing the action
while (true)
{
CheckIfCancelled(cancelToken);

Task subTask = Task.Factory.StartNew(wrapperAction, cancelToken, subTaskCreationOptions, TaskScheduler.Current);

if (synchronous)
{
stopWatch.Start();
try
{
subTask.Wait(cancelToken);
}
catch { /* do not let an errant subtask to kill the periodic task...*/ }
stopWatch.Stop();
}

// use the same Timeout setting as the System.Threading.Timer, infinite timeout will execute only one iteration.
if (intervalInMilliseconds == Timeout.Infinite) { break; }

iteration++;

if (maxIterations > 0 && iteration >= maxIterations) { break; }

try
{
stopWatch.Start();
periodResetEvent.Wait(intervalInMilliseconds, cancelToken);
stopWatch.Stop();
}
finally
{
periodResetEvent.Reset();
}

CheckIfCancelled(cancelToken);

if (duration > 0 && stopWatch.ElapsedMilliseconds >= duration) { break; }
}
}
}

/// <summary>
/// Checks if cancelled.
/// </summary>
/// <param name="cancelToken">The cancel token.</param>
private static void CheckIfCancelled(CancellationToken cancellationToken)
{
if (cancellationToken == null)
throw new ArgumentNullException("cancellationToken");

cancellationToken.ThrowIfCancellationRequested();
}
}
}


خروجی:

2/18/2013 4:17:13 PM
2/18/2013 4:17:15 PM
2/18/2013 4:17:17 PM
2/18/2013 4:17:19 PM
2/18/2013 4:17:21 PM
2/18/2013 4:17:23 PM
2/18/2013 4:17:25 PM
2/18/2013 4:17:27 PM
2/18/2013 4:17:29 PM
2/18/2013 4:17:31 PM
Finished!
Press any key to continue . . .