Click here to Skip to main content
16,015,583 members
Home / Discussions / C#
   

C#

 
GeneralRe: .NET 1.1 and Visual Studio 2005 Pin
Spacix One25-Apr-08 2:04
Spacix One25-Apr-08 2:04 
GeneralRe: .NET 1.1 and Visual Studio 2005 Pin
Ajay.k_Singh25-Apr-08 3:58
Ajay.k_Singh25-Apr-08 3:58 
GeneralRe: .NET 1.1 and Visual Studio 2005 Pin
YAI25-Apr-08 21:43
YAI25-Apr-08 21:43 
GeneralSystem.OverflowException with TeeChart Pin
Peterson Luiz24-Apr-08 15:16
Peterson Luiz24-Apr-08 15:16 
GeneralRe: System.OverflowException with TeeChart Pin
Christian Graus24-Apr-08 15:22
protectorChristian Graus24-Apr-08 15:22 
GeneralRe: System.OverflowException with TeeChart Pin
Peterson Luiz24-Apr-08 15:28
Peterson Luiz24-Apr-08 15:28 
GeneralRe: System.OverflowException with TeeChart Pin
Christian Graus24-Apr-08 15:31
protectorChristian Graus24-Apr-08 15:31 
GeneralDelayed request while waiting for data over an internet connection Pin
MAW3024-Apr-08 15:15
MAW3024-Apr-08 15:15 
My program request data over the internet, I need to request this data back to back using the same array and changing the data within it (basically the handle). When I get the data coming from the internet I have already removed the handle from my array and cleared the info from the program through which I requested it. I found the following code (see below) in the help files for delaying the request until I receive the first request. But this does not work either, was this designed for use over the internet or just on the local computer. How can I have my request delayed until I have received all data.

Thanks in advance,
Michael


// Timer
The Elapsed event is raised on a ThreadPool thread, so the event-handling
method might run on one thread at the same time that a call to the Stop
method runs on another thread. This might result in the Elapsed event being
raised after the Stop method is called. The code example for this topic shows
one way to prevent the race condition.

Example
The following code example shows one way to prevent the thread that calls the
Stop method from continuing until a currently executing Elapsed event ends,
and also to prevent two Elapsed events from executing the event handler at the
same time (often referred to as reentrancy).

The example executes 100 test runs. Each time the test is run, the timer is
started with an interval of 150 milliseconds. The event handler uses the
Thread.Sleep method to simulate a task that randomly varies in length from 50
to 200 milliseconds. The test method also starts a control thread that waits
for a second and then stops the timer. If an event is being handled when the
control thread stops the timer, the control thread must wait until the event
is finished before proceeding.

The Interlocked.CompareExchange(Int32,Int32,Int32) method overload is used to
avoid reentrancy and to prevent the control thread from continuing until an
executing event ends. The event handler uses the CompareExchange(Int32,Int32,
Int32) method to set a control variable to 1, but only if the value is
currently zero. This is an atomic operation. If the return value is zero, the
control variable has been set to 1 and the event handler proceeds. If the
return value is non-zero, the event is simply discarded to avoid reentrancy.
(If it were necessary to execute every event, the Monitor class would be a
better way to synchronize the events.) When the event handler ends, it sets
the control variable back to zero. The example records the total number of
events that executed, that were discarded because of reentrancy, and that
occurred after the Stop method was called.

The control thread uses the CompareExchange(Int32,Int32,Int32) method to set
the control variable to -1 (minus one), but only if the value is currently
zero. If the atomic operation returns non-zero, an event is currently
executing. The control thread waits and tries again. The example records the
number of times the control thread had to wait for an event to finish.

[MTAThread]
public static void Main()
{
Timer1.Elapsed += new ElapsedEventHandler(Timer1_ElapsedEventHandler);
Timer1.Interval = timerInterval;

Console.WriteLine();
for(int i = 1; i <= testRuns; i++)
{
TestRun();
Console.Write("\rTest {0}/{1} ", i, testRuns);
}

Console.WriteLine("{0} test runs completed.", testRuns);
Console.WriteLine("{0} events were raised.", numEvents);
Console.WriteLine("{0} events executed.", numExecuted);
Console.WriteLine("{0} events were skipped for concurrency.", numSkipped);
Console.WriteLine("{0} events were skipped because they were late.", numLate);
Console.WriteLine("Control thread waited {0} times for an event to complete.", numWaits);
}

public static void TestRun()
{
// Set syncPoint to zero before starting the test
// run.
syncPoint = 0;

Timer1.Enabled = true;

// Start the control thread that shuts off the timer.
Thread t = new Thread(ControlThreadProc);
t.Start();

// Wait until the control thread is done before proceeding.
// This keeps the test runs from overlapping.
t.Join();

}
QuestionSetup and deployment Pin
jharker198724-Apr-08 13:03
jharker198724-Apr-08 13:03 
GeneralRe: Setup and deployment Pin
Christian Graus24-Apr-08 13:06
protectorChristian Graus24-Apr-08 13:06 
GeneralRe: Setup and deployment Pin
jharker198724-Apr-08 13:07
jharker198724-Apr-08 13:07 
GeneralRe: Setup and deployment Pin
Spacix One24-Apr-08 13:23
Spacix One24-Apr-08 13:23 
GeneralRe: Setup and deployment Pin
jharker198725-Apr-08 2:26
jharker198725-Apr-08 2:26 
GeneralRe: Setup and deployment Pin
mav.northwind25-Apr-08 22:20
mav.northwind25-Apr-08 22:20 
GeneralListview interaction scheme question Pin
Spacix One24-Apr-08 12:55
Spacix One24-Apr-08 12:55 
QuestionNeed help with multi threaded modular application Pin
darthBug24-Apr-08 9:51
darthBug24-Apr-08 9:51 
GeneralRe: Need help with multi threaded modular application Pin
Luc Pattyn24-Apr-08 11:06
sitebuilderLuc Pattyn24-Apr-08 11:06 
GeneralRe: Need help with multi threaded modular application Pin
carbon_golem24-Apr-08 13:32
carbon_golem24-Apr-08 13:32 
GeneralRe: Need help with multi threaded modular application Pin
darthBug25-Apr-08 1:07
darthBug25-Apr-08 1:07 
GeneralRe: Need help with multi threaded modular application Pin
carbon_golem25-Apr-08 3:30
carbon_golem25-Apr-08 3:30 
QuestionEnumeration Location Pin
snorkie24-Apr-08 9:42
professionalsnorkie24-Apr-08 9:42 
GeneralRe: Enumeration Location Pin
Broken Bokken24-Apr-08 9:47
Broken Bokken24-Apr-08 9:47 
GeneralRe: Enumeration Location Pin
darthBug24-Apr-08 10:01
darthBug24-Apr-08 10:01 
GeneralRe: Enumeration Location Pin
snorkie24-Apr-08 10:08
professionalsnorkie24-Apr-08 10:08 
GeneralRe: Enumeration Location Pin
carbon_golem24-Apr-08 10:53
carbon_golem24-Apr-08 10:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.