472,980 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Run Thread when form open.Plz urgent.



Please give a kind look on my question. and please comments.

I am Using C# as development tool of Dot Net 2.0.
I wrote a function read_data() that read data from Serial Port
continuously.Till application gets close.
But When Form loaded read_data() function take tooo much CPU. Even I can
not see Application controls.
Some body told me, Use Threading.

I need your help. I have used Thread just for study purpose. Not for
Professional application.

(I used in Main () function for a dummy application.)

But i don't know how to implement Threads, When a Form gets load?

I would be grateful if you would just mention frew lines.

That supports/give me hint...

Sincerely,

Mr. Khurram Nazir.

*** Sent via Developersdex http://www.developersdex.com ***
Feb 7 '07 #1
2 3432
Hi,

Typically in the form's Page_Load event you create a Thread and start it,
keep a handle to this thread since you have to end it when your form
closes. The Thread reads from the serial port and invoke your GUI-thread
when it has data to post. Read Jon Skeet's article on multi-threading and
pay particular attention to page 7 (Threading in Windows Forms).

[Multi-threading in .NET: Introduction and suggestions]
http://www.yoda.arachsys.com/csharp/threads/
On Wed, 07 Feb 2007 12:53:27 +0100, abid gee <pk****@yahoo.comwrote:
>

Please give a kind look on my question. and please comments.

I am Using C# as development tool of Dot Net 2.0.
I wrote a function read_data() that read data from Serial Port
continuously.Till application gets close.
But When Form loaded read_data() function take tooo much CPU. Even I can
not see Application controls.
Some body told me, Use Threading.
I need your help. I have used Thread just for study purpose. Not for
Professional application.

(I used in Main () function for a dummy application.)
But i don't know how to implement Threads, When a Form gets load?
I would be grateful if you would just mention frew lines.

That supports/give me hint...

Sincerely,

Mr. Khurram Nazir.

*** Sent via Developersdex http://www.developersdex.com ***


--
Happy Coding!
Morten Wennevik [C# MVP]
Feb 7 '07 #2
Hi Khurram,

Sounds like you have 2 problems:
1) your read_data function is pegging the CPU
2) you are not servicing the message pump on your UI thread (which is
why your UI is not repainting)

To solve #1, you might want to consider putting in a sleep. Hard to
say without knowing what read_data does
To solve #2, you should create a separate "worker" thread for
read_data(), something like this:

private static bool _continue = true;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);

Thread thread = new Thread(Program.read_data);
thread.Start();

Application.Run(new Form1());

_continue = false;
}

private static void read_data()
{
while (_continue)
{
// <put your worker code here>

// you might want to consider sleeping like this if you don't want
to peg the CPU
Thread.Sleep(1000);
}
}

There are some more sophisticated things you can do with inter-thread
communication (e.g. have the main thread tell the other thread to
pause processing, etc...), but this should get you started in the
right direction.

Hope that helps,
John

On Feb 7, 6:53 am, abid gee <pk_...@yahoo.comwrote:
Please give a kind look on my question. and please comments.

I am Using C# as development tool of Dot Net 2.0.
I wrote a function read_data() that read data from Serial Port
continuously.Till application gets close.
But When Form loaded read_data() function take tooo much CPU. Even I can
not see Application controls.
Some body told me, Use Threading.

I need your help. I have used Thread just for study purpose. Not for
Professional application.

(I used in Main () function for a dummy application.)

But i don't know how to implement Threads, When a Form gets load?

I would be grateful if you would just mention frew lines.

That supports/give me hint...

Sincerely,

Mr. Khurram Nazir.

*** Sent via Developersdexhttp://www.developersdex.com***

Feb 7 '07 #3

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

Similar topics

0
by: Janwillem Borleffs | last post by:
----------------------------------------------------------------- This is the FAQ thread where the FAQ compilation project goes. * If you wish to improve the contents, please copy the whole...
0
by: Janwillem Borleffs | last post by:
----------------------------------------------------------------- This is the FAQ thread where the FAQ compilation project goes. * If you wish to improve the contents, please copy the whole...
3
by: R. Rajesh Jeba Anbiah | last post by:
----------------------------------------------------------------- This is the FAQ thread where the FAQ compilation project goes. * If you wish to improve the contents, please copy the whole...
5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
3
by: Jack | last post by:
I have an application (COM Add-in) that in the initialization code it displays a splash screen. I want the splash screen to close after n seconds , so I add a timer to the splash screen and...
6
by: Joe | last post by:
I've been getting this message at all different times since a few days ago. I understand the message but not why I get it. I don't have any other threads. This happens when closing dialog boxes,...
6
by: =?Utf-8?B?Q2h1Y2sgUA==?= | last post by:
If I launch a thread within an asp.net application. Is their anyway to get at the base threads HttpContext object. If the thread errors I want to capture stuff like:...
9
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form...
6
by: Steve | last post by:
Hi All I have an on-screen keyboard within a POS program I have written in VB.net 2005, for touch screen computers I have it set to 'always on top' so the user can move the cursor to...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.