468,511 Members | 1,532 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,511 developers. It's quick & easy.

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 3272
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Janwillem Borleffs | last post: by
reply views Thread by Janwillem Borleffs | last post: by
3 posts views Thread by R. Rajesh Jeba Anbiah | last post: by
3 posts views Thread by Jack | last post: by
6 posts views Thread by =?Utf-8?B?Q2h1Y2sgUA==?= | last post: by
6 posts views Thread by Steve | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.