473,403 Members | 2,338 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,403 software developers and data experts.

Threading Issue : "app.thread Event handler not working properly"

Hi,
My application creates a thread for the conversion of several strings. I have an eventhandler called app.finished, but unfortunately, it is never called. Can any one tell me why?

The code is given below for the classes which will used for threading purpose..
Expand|Select|Wrap|Line Numbers
  1. public class ThreadStarter
  2.     {
  3.  
  4.               [STAThread]
  5.         public static void main(string[] args)
  6.         {
  7.             Random rnd = new Random(); 
  8.  
  9.             for (int j = 0; j < 5; j++)
  10.             {
  11.                 App.Threads.Add(new Convertor());
  12.             }
  13.  
  14.             Init();
  15.  
  16.             App.Start();
  17.  
  18.             Thread.Sleep(600);
  19.  
  20.             App.Stop();
  21.         }
  22.  
  23.         private static void Init()
  24.         {
  25.             App.Connection = new GConnection("localhost", 9000,"user", "user");
  26.             // grid thread needs to
  27.             if (App.Manifest.Count == 0)
  28.             {
  29.                 App.Manifest.Add(new ModuleDependency(typeof(Convertor).Module));
  30.             }
  31.             else
  32.             {
  33.  
  34.             }
  35.  
  36.  
  37.             // subscribe to ThreadFinish event
  38.             App.ThreadFinish += new GThreadFinish(App_ThreadFinish);
  39.         }
  40.  
  41.         static void App_ThreadFinish(GThread thread)
  42.         {
  43.             DataSet1TableAdapters.SensorValuesTableAdapter senValAdapt = new Data_Repositiry_and_Terminal.DataSet1TableAdapters.SensorValuesTableAdapter();
  44.             Convertor cs = (Convertor)thread;
  45.             //cs.updateDB();
  46.             List<string> val = cs.getVal();
  47.  
  48.         }
  49.     }
  50. }
  51.  
  52.  
and the class which overrides the main function is
Expand|Select|Wrap|Line Numbers
  1. [Serializable]
  2.     class Convertor : GThread
  3.     {
  4.         private List<string> values = null;
  5.         public static DataSet sqlData;
  6.         //private DataSet1TableAdapters.SensorValuesTableAdapter senValAdapt;
  7.  
  8.  
  9.         public Convertor()
  10.         {
  11.             sqlData = new DataSet();
  12.             // For the purpose of inserting data into database
  13.           //senValAdapt = new Data_Repositiry_and_Terminal.DataSet1TableAdapters.SensorValuesTableAdapter(); 
  14.         }
  15.  
  16.         public override void Start()
  17.         {
  18.             DataRow R = null;
  19.  
  20.             for (int i = 0; i < sqlData.Tables["SensorDataPackets"].Rows.Count; i++)
  21.             {
  22.  
  23.                 R = sqlData.Tables["SensorDataPackets"].Rows[i];
  24.                 string reading;
  25.  
  26.                 reading = R.ItemArray.GetValue(1).ToString();
  27.  
  28.                 Int32 RawTemp = 0;
  29.                 Int32 RawHum = 0;
  30.                 Int32 RawLight = 0;
  31.                 Int32 SeqNumber = 0;
  32.                 double TempinDegC = 0;
  33.                 double RelHuminPer = 0;
  34.                 double LightinLx = 0;
  35.                 string NodeID, Temperature, Humidity, Light, sequenceNumber;
  36.  
  37.  
  38.                 // Node ID
  39.                 NodeID = reading.Substring(28, 4);
  40.                 //      NodeID = NodeID + reading.Substring(45, 2);
  41.  
  42.                 //Sequence Number
  43.                 sequenceNumber = reading.Substring(32, 4);
  44.  
  45.                 // Light Reading
  46.                 Light = reading.Substring(36, 4);
  47.                 //  Light = Light + reading.Substring(57, 2);
  48.  
  49.                 // Temperature Reading
  50.                 Temperature = reading.Substring(40, 4);
  51.                 //  Temperature = Temperature + reading.Substring(63, 2);
  52.  
  53.                 // Humidity Reading
  54.                 Humidity = reading.Substring(44, 4);
  55.                 //Humidity = Humidity + reading.Substring(69, 2);
  56.  
  57.  
  58.                 //Converting string into Decimal value
  59.                 RawTemp = Convert.ToInt32(Temperature, 16);
  60.                 RawHum = Convert.ToInt32(Humidity, 16);
  61.                 RawLight = Convert.ToInt32(Light, 16);
  62.                 SeqNumber = Convert.ToInt32(sequenceNumber, 16);
  63.  
  64.  
  65.                 // Converting Raw Temperature into Degree Centigrade
  66.                 TempinDegC = 0.01 * RawTemp - 39.6;
  67.  
  68.                 // Converting Raw Humidity into Relative Humidity in Percentage
  69.                 RelHuminPer = (TempinDegC - 25) * (0.01 + 0.00008 * RawHum) + (-4 + 0.0405 * RawHum - 0.0000028 * RawHum * RawHum);
  70.  
  71.                 // Converting Raw Light into Lux unit
  72.                 double V = (float)RawLight / 4096 * 1.5;
  73.                 double I = V / 100000;
  74.  
  75.                 LightinLx = 1e6 * I * 1000;
  76.                 values.Add(NodeID);
  77.                 values.Add(TempinDegC.ToString());
  78.                 values.Add(RelHuminPer.ToString());
  79.                 values.Add(LightinLx.ToString());
  80.                 values.Add((R.ItemArray.GetValue(2)).ToString());
  81.                 values.Add((R.ItemArray.GetValue(0)).ToString());
  82.                 values.Add(SeqNumber.ToString());
  83.            }
  84. }
  85.  
This is just one thing.

Other than this I alse need to retrieve the value of the converted data and save it into the database.
Can any one provide me some professional assistance?

Thank you,

Regards,
Syed Ahmed Hussain
Aug 5 '09 #1
7 2080
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1.               [STAThread]
  2.         public static void main(string[] args)
  3.         {
  4.             Random rnd = new Random(); 
  5.  
  6.             for (int j = 0; j < 5; j++)
  7.             {
  8.                 App.Threads.Add(new Convertor());
  9.             }
  10.  
  11.             Init();
  12.  
  13.             App.Start();
  14.  
  15.             Thread.Sleep(600);
  16.  
  17.             App.Stop();
  18.         }
  19.  
Maybe I'm blind before my first coffee in the morning but are you trying to use an object called App without having first defining/creating it?
Aug 5 '09 #2
The part which you are looking at is not mine... That was the part which was given to me written by some other guy... And Because I dont know what he did. SO I just used it :D .... The class convertor is written by me ... :( ..
Aug 5 '09 #3
tlhintoq
3,525 Expert 2GB
I have an eventhandler called app.finished, but unfortunately, it is never called. Can any one tell me why?
Maybe I'm blind before my first coffee in the morning but are you trying to use an object called App without having first defining/creating it?
The part which you are looking at is not mine... That was the part which was given to me written by some other guy... And Because I dont know what he did. SO I just used it :D .... The class convertor is written by me ... :( ..
I think this answers your original question of "why doesn't it work?" Because you got a chunk of code that you don't understand, that really doesn't relate to your project, using objects you don't create and are just hoping that somehow it's all going to magically fit together and work.

There is no reason to have any kind of overrides in what you are doing. You really are doing anything that needs to be a class inheriting from another class, which is where you most commonly use an override.

I would suggest you start smaller. Before you worry about getting the readings from your weather sensor, just work on threading. Make a tiny application that does threading for frivolous things like adding two numbers.

Suggestion: Instead of having all of your sensor readings as one long method called "start"... have a method for each sensor, then you can call them easily from anywhere in your application. Like this you have to read all the sensors together. What if you want to read the windspeed twice as often as the temperature?

Expand|Select|Wrap|Line Numbers
  1. ReadSensors()
  2. {
  3.    string Temp = ReadTemp();
  4.    string Humidity = ReadHumidity();
  5.    // etc.
  6. }
  7.  
Aug 5 '09 #4
I think this answers your original question of "why doesn't it work?" Because you got a chunk of code that you don't understand, that really doesn't relate to your project, using objects you don't create and are just hoping that somehow it's all going to magically fit together and work.
Ouch that hurts!!!!
Buddy the only problem is that I am working under somebody else who is asking me to use this logic instead of creating the whole thing from scratch...

I would suggest you start smaller. Before you worry about getting the readings from your weather sensor, just work on threading. Make a tiny application that does threading for frivolous things like adding two numbers.
This hurts even more :@
I totally agree with you that I dont have knowledge for thread based programming, because I am doing it for the first time in my entire life, but with the exception of I already had done what you are telling me (Making of these tiny applications) but again its not me who's the boss of this all..

I can do two things, not post this code any place and bang my head on the wall or else I should use help of some one who is well educated like you ...

I actually wanted to write an article based on S/W house and R&D departments of different Regions .... but for right now thanks for the advice :)
Aug 5 '09 #5
Because you got a chunk of code that you don't understand, that really doesn't relate to your project, using objects you don't create and are just hoping that somehow it's all going to magically fit together and work.
Tell me if the code is written all wrong or it is wrong.... Because then I will start writing it again from the scratch and also do some sample programs for learning thread based programming as well..
Aug 5 '09 #6
tlhintoq
3,525 Expert 2GB
Terribly sorry if some of those comments hurt your feelings.
If you have no choice but to use the code your boss provided then I suggest using it as it is nice to stay employed and buy food.

Since you have no choice but to use it, you have no choice but to understand it and clean it up.
Expand|Select|Wrap|Line Numbers
  1.               [STAThread]
  2.         public static void main(string[] args)
  3.         {
  4.             Random rnd = new Random(); 
  5.  
  6.             for (int j = 0; j < 5; j++)
  7.             {
  8.                 App.Threads.Add(new Convertor());
  9.             }
  10.  
  11.             Init(); 
  12.             App.Start(); 
  13.             Thread.Sleep(600);
  14.             App.Stop();
  15.         }
  16.  
Line 4: Makes a new Random. Why? It is never used.
Line 13: Sleeps the current thread for 600 milliseconds. Why?
Lines 8, 12, 14 all make use of an App ... What is it? Where does it get initialized?
These are probably all things you need to make sure you have a complete understanding of. Since they came from the boss: Ask him.

Expand|Select|Wrap|Line Numbers
  1.     class Convertor : GThread
  2.  
Your converter is inheriting from class GThread which appears nowhere in the code you posted, so there is nothing anyone here can tell about that. I can point out that your override of Start() never calls base.Start. So if there is some initialization that needs to happen there it doesn't happen.

I have an eventhandler called app.finished, but unfortunately, it is never called. Can any one tell me why?
Expand|Select|Wrap|Line Numbers
  1.             // subscribe to ThreadFinish event
  2.             App.ThreadFinish += new GThreadFinish(App_ThreadFinish);
  3.  
This is the line at attaches the handler to the App.ThreadFinish event. If the handler doesn't get called, then one of two things is to blame:
1) The App.ThreadFinish event doesn't fire.
2) This line that does the attaching is never called, so you don't attach to the event. This line is part of the Init() rountine called from ThreadStarter.Main

Other than this I alse need to retrieve the value of the converted data and save it into the database.
Can any one provide me some professional assistance?
Please read both parts 1 & 2 of these excellent articles on using a database in your applications.
http://bytes.com/topic/net/insights/...rogram-part-ii
Aug 5 '09 #7
Its OK dear .... I dont have any problems with what you have said.,... :)

Well actually I am using Alchemi a software for enabling grid computing. To use Alchemi for grid based computing in your code you have to use a Alchemi.core.dll. This code actually refers to the Alchemi's Code. I tried to find it on many sites, but for alchemi I only got the same result that without creating an object of app writing codes.

Please read both parts 1 & 2 of these excellent articles on using a database in your applications.
How To Use A Database In Your Program Part II
And about the DB I know how to CRUD.. What I meant was taking back the values from the threads direct into the DB.. Is it possible?
Aug 6 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: lovecreatesbeauty | last post by:
Hello, I want to know whether programming languages such as C or C++ provide something like "event". In function "f1", a handler/function "h2" is triggered. But f1 won't be blocked by h2,...
1
by: VMI | last post by:
Im using a separate thread in my code to copy a huge file, but I get the "Controls created on one thread cannot be parented to a control on a different thread" when I want to update the Form...
2
by: Sakharam Phapale | last post by:
Hi All, I am working on a project, where maximum operations carried out on Files and multi-dimensional arrays. Since array data is huge application takes too much memory. My problem is, after...
4
by: Lars Netzel | last post by:
Is there any way to add some code that will happen in every single Event that i raised in a form? I have a Total field in the bottom of a pretty complex page and I think it's kind of "Dirty" to...
22
by: Brett | last post by:
I have a second thread, t2, that errors out and will stop. It's status is then "Stopped". I try to start t2 from thread 1, t1, by checking If t2.threadstate = "Stopped" Then t2.start() ...
3
by: JohnR | last post by:
I have a form with a number of text boxes, comboboxes etc. What I would like to do is create an event handler for the "mouseenter" event for each of the controls whereby I display information...
15
by: Brady Love | last post by:
I am currently working an an app that will post and edit blogs on blogger. Right now I have it so I can recive a list of the blogs and the content of those blogs using Atomizer. This is my first...
3
by: NaeiKinDus | last post by:
Hello, i'm trying to program a thread that would be locked (by a mutex) and that would only be unlocked once that a function (generating data) is done. The purpose is to generate data, and unlock...
0
by: Schadrach | last post by:
I'm having a strange problem, I have a small executable that runs a backup for a some data nightly, and as of September 22, 2007 it has ceased to function with the following error: Unhandled...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.