473,378 Members | 1,496 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,378 software developers and data experts.

Threads problem

Hi guys,
I would like some advice on thread programming using C# so I wanted
some advice.

I am writing an application that communicates with a panel over
ethernet, collects data and writes it to a file. The way the data is
collected is that we have different schedules (so one set of data is
collected say every second, another set of data might be collected
every 30 seconds, and so on).

The way I am approaching this is that I have created a class called
Scheduler, which consists of an ArrayList which has the individual
schedules. Each individual schedule has a timer object which would fire
after a specified amount of time (again this would be different for
each different schedule). When it fires, it would set a flag. In the
scheduler I have a function called Run, which goes through each
individual schedule item and see if the flag is set. If it is, it would
call a method in the individual schedule (called DoServicing()) and
clear the flag. The DoServicing() method calls the communication
function and then writes the flag.

Now I am creating the scheduler on a separate thread when the user
presses a Start button, and the scheduler just keeps calling Run()
forever, until the user presses a stop button. The problem I am running
into is that it seems like when the scheduler is in the DoServicing()
method (on its own thread), often times the application would switch
threads (back to the main UI thread). So whats happening is that the
file is not getting written. At other times, if there are 2
communication messages in DoServicing() its doing only 1 before
switching threads back to the main UI thread. Interestingly enough, if
I had no communication messages (just writing to file) everything works
perfectly according to the given schedule. As soon as I put the
communication messages in the DoServicing() method, it seems to switch
threads before going through the whole method.

In this sort of application, where the data must be collected and
written exactly at user specified intervals, obviously I need more
control on the thread on which the scheduler is running. What I would
like is that the scheduler should be continuously running according to
the given individual schedules (communicating to the panel and writing
the files). How do I achieve that?

Note: I have already tried lock around the entire body of the
DoServicing() method, as well as setting the thread priority of the
scheduler thread to highest. Neither of them worked.

Nov 17 '05 #1
1 1218
Hi,

I'd have one System.Threading.Timer instance that would go through the
schedules every second and fire off tasks due now on the thread pool (see
System.Threading.ThreadPool).

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
<Ra*******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi guys,
I would like some advice on thread programming using C# so I wanted
some advice.

I am writing an application that communicates with a panel over
ethernet, collects data and writes it to a file. The way the data is
collected is that we have different schedules (so one set of data is
collected say every second, another set of data might be collected
every 30 seconds, and so on).

The way I am approaching this is that I have created a class called
Scheduler, which consists of an ArrayList which has the individual
schedules. Each individual schedule has a timer object which would fire
after a specified amount of time (again this would be different for
each different schedule). When it fires, it would set a flag. In the
scheduler I have a function called Run, which goes through each
individual schedule item and see if the flag is set. If it is, it would
call a method in the individual schedule (called DoServicing()) and
clear the flag. The DoServicing() method calls the communication
function and then writes the flag.

Now I am creating the scheduler on a separate thread when the user
presses a Start button, and the scheduler just keeps calling Run()
forever, until the user presses a stop button. The problem I am running
into is that it seems like when the scheduler is in the DoServicing()
method (on its own thread), often times the application would switch
threads (back to the main UI thread). So whats happening is that the
file is not getting written. At other times, if there are 2
communication messages in DoServicing() its doing only 1 before
switching threads back to the main UI thread. Interestingly enough, if
I had no communication messages (just writing to file) everything works
perfectly according to the given schedule. As soon as I put the
communication messages in the DoServicing() method, it seems to switch
threads before going through the whole method.

In this sort of application, where the data must be collected and
written exactly at user specified intervals, obviously I need more
control on the thread on which the scheduler is running. What I would
like is that the scheduler should be continuously running according to
the given individual schedules (communicating to the panel and writing
the files). How do I achieve that?

Note: I have already tried lock around the entire body of the
DoServicing() method, as well as setting the thread priority of the
scheduler thread to highest. Neither of them worked.


Nov 17 '05 #2

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

Similar topics

0
by: Gonçalo Rodrigues | last post by:
Hi, I have a problem with threads and sockets. I'll try to describe the problem in words with pseudo-code. I've been working on a few classes to make it easier to work with threads. This...
3
by: Sebastian Meyer | last post by:
Hi Newsgroup, i have some problems with using threads and signals in one program. In my program i have three threads running, one for checking a directory at a specified interval to see if new...
8
by: Alban Hertroys | last post by:
Hello, I'm using psycopg to insert records in a number of threads. After the threads finish, another thread runs to collect the inserted data. Now, my problem is that psycopg let's my threads...
11
by: Przemysław Różycki | last post by:
Hello, I have written some code, which creates many threads for each connection ('main connection'). The purpose of this code is to balance the load between several connections ('pipes'). The...
5
by: Russell Warren | last post by:
Does anyone know the scope of the socket.setdefaulttimeout call? Is it a cross-process/system setting or does it stay local in the application in which it is called? I've been testing this and...
9
by: Arafangion | last post by:
Hello, recently I've been trying to figure out how the heck to just destroy a thread. I have since rewritten the code in question, but what I was trying to do was to use the .Abort() method, but...
6
by: RahimAsif | last post by:
Hi guys, I would like some advice on thread programming using C#. I am writing an application that communicates with a panel over ethernet, collects data and writes it to a file. The way the...
11
by: catsup | last post by:
Hi, I have an app written under version Python 2.3.5. The problem I'm having is that it hangs on one of its threads. The thread that hangs does updates to a standard dictionary shared with...
10
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005 and .net 2.0. I started 2 threadpool threads. How do I know when they're done with their tasks? Thanks. ThreadPool.QueueUserWorkItem(new...
18
by: Jon Slaughter | last post by:
"Instead of just waiting for its time slice to expire, a thread can block each time it initiates a time-consuming activity in another thread until the activity finishes. This is better than...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.