I am delevoping a Scheduler module whose functionality is to load the Scheduled Item Details every day (For this I have created a System.Threading.TimerCallBack with Timespan 1 day) in a stack. Once the stack is loaded I look for the qualified items every one minute (I create an another TimerCallBack thread). This Scheuler module needs to be started when the UI interface gets loaded.
The issue lies in starting the shceduler since the EventHandler or Delegate or ThreadPool that I create to start the scheduler goes out of scope in the UI.
Hers is the code snippet:
/*** From UI - Initiate the Scheduler Module *****/
Private Sub btnInit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Invoke(EventHandler_To_StartScheduler)
End Sub
Private Sub EventHandler_To_StartScheduler(ByVal sender As Object, ByVal e As EventArgs)
Try
System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf InitiateScheduler), bStatus)
Catch ex As Exception
End Try
End Sub
Private Sub InitiateScheduler(ByVal objState As Object)
Dim bFlag As Boolean = False
Try
bFlag = Scheduler_DLL.Init(objState)
Catch ex As Exception
End Try
End Sub
/***** Scheduler DLL *****/
public void Init(Object stateInfo)
{
bool bFlag = false;
try
{
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
TimeSpan timeSpan1 = System.DateTime.Today.AddDays(1) - System.DateTime.Now;
TimeSpan timeSpanInterval = new TimeSpan(1, 0, 0, 0); // Interval - One day
TimerCallback tCallBack = new TimerCallback(StartProcess);
Timer timerSched = new Timer(tCallBack, autoEvent, new TimeSpan(0), new TimeSpan(1, 0, 0));
timerSched.Change(timeSpan1, timeSpanInterval);
bFlag = autoEvent.WaitOne();
}
catch (ThreadAbortException thEx)
{
//Do Nothing
}
catch (Exception ex)
{
throw ex;
}
}
private void StartProcess(Object stateObj)
{
try
{
AutoResetEvent autoEvent = (AutoResetEvent)stateObj;
System.DateTime dtScheduleDate = System.DateTime.Today.Date;
GetDataScheduleDetails(dtScheduleDate , ref dataTableSchedules);
if (dataTableSchedules != null && dataTableSchedules.Rows.Count 0)
{
TestForAnyDuplicates(ref dataTableSchedules,ref dataTableVerifiedSchedules);
}
if (dataTableVerifiedSchedules != null && dataTableVerifiedSchedules.Rows.Count 0)
{
dataTableSchedules.Rows.Clear();
RunTheSchedule();
autoEvent.Set();
}
else
{
//bFlag = true;
bSchedProcessStatus = true;
autoEvent.Set();
}
}
catch (Exception ex)
{
string strErrMsg = ex.Message;
}
}
private void RunTheSchedule()
{
try
{
Timer timerCheckEveryMinute = new Timer(new TimerCallback(CheckEveryMinute), new Object(), new TimeSpan(0), new TimeSpan(0, 1, 0));
}
catch (Exception ex)
{
throw ex;
}
}
private void CheckEveryMinute(Object stateInfo)
{
System.DateTime dtCurrentDateTime = System.DateTime.Now;
try
{
if (dataTableVerifiedSchedules != null && dataTableVerifiedSchedules.Rows.Count 0)
{
DataView dvScheduleItemNow = new DataView(dataTableVerifiedSchedules, "TimeValue = #" + dtCurrentDateTime + "#", "TimeValue", DataViewRowState.CurrentRows);
if (dvScheduleItemNow != null && dvScheduleItemNow .Count 0)
{
foreach (DataRow trScheduleItem in dvScheduleItemNow .Table.Rows)
{
RunScheduledItem( (int) trScheduleItem["SchedItemIdentifier"] );
}
}
}
}
catch (Exception ex)
{
string strErrMsg = "::ERR::"+ex.Message;
}
}
The issue I have is to get the control go to the Scheduler Module and execute the code to populate the data in the stack and to run the shcedules items at the appropriate time if any in run time beofre the UI method which invokes goes out of scope.
I have checked my code when I am running the code in the debugger with breakpoints at every possible place and I didn't have any issues.
[Note: The code I have posted is truncated for better readability and please ignore any syntax errors in the code.]
Any hint to resolve this issue will be appreciated.
-Thanks in advance
RKB.
EggHeadCafe - .NET Developer Portal of Choice
http://www.eggheadcafe.com