473,411 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Invoking a delegate to start aSystem.Threading.TimerCallBack thread

R K
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
Nov 9 '07 #1
0 3485

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michel Schilthuizen | last post by:
Hi, I am working on an application that can use functionlity in some sort of plugins. I have implemented this by using Assembly.LoadFrom and MethodInfo.GetMethods. The calling of the plugins...
0
by: Eric Sabine | last post by:
OK, I'm trying to further my understanding of threading. The code below I wrote as kind of a primer to myself and maybe a template that I could use in the future. What I tried to do was pass data...
2
by: KSC | last post by:
Hello, I have used a thread timer as in the documentation on MSDN in my VB.NET application. Using System.Threading.Interlocked.Increment I increment the counter to a certain point, perform an...
25
by: MuZZy | last post by:
Hi, I'm currently rewriting some functionality which was using multithredaing for retrieving datasets from database and updating a grid control. I found that the grids (Infragistics UltraGrid,...
5
by: greg.merideth | last post by:
I have a class that I've provided an event for to be called when the processing in the class is complete (a callback). The class spins up a series of threads for the background operation and I'm...
6
by: Sergey Poberezovskiy | last post by:
I have the following code in C# that I have trouble converting to VB(2.0): private delegate void openDialog(); private void openWindowsDialog(openDialog open) { Thread thread = new Thread(new...
6
by: DaTurk | last post by:
I know in a WinForm you can check if an Invoke is inquired and invoke a method on the main thread. But can you do this in a class? Can I call Invoke(someMethod) in my asynch event handler? And...
3
by: Curious | last post by:
Hi, I have a program that gets the prices of stocks in real-time. The program consists of the following key components: // Constructor public GetRealTimeStockPrice() { // Step 1
5
by: hurricane_number_one | last post by:
I'm trying to have a class, which uses threads be able to raise events to the form that created it. I've seen solutions around the net for this, but I didn't really like their implementation. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.