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

Thread Abort and DataGrid Question

Hi,

In my Windows application, I'm using thread, and I've 2 questions:

1. I read that it's not recommended to use Thread.Abort(), so what is the
best way to close the thread, in case the user wants to cancel or the thread
is finished and i want to close my application?

2. in my Thread I'm creating a dataGridView with DataTable as dataSoruce,
and I've problem with the scroll bars, I see the scroll bars but both of the
are frozen and i can't scroll between the lines, but if i'm using the middle
button in my mouse it does scroll. I took my code to another Win Form without
thread and i don't have this problem there, so my guess is that it's releated
to thread.
Does anyone have any idea?

Thanks,
Gidi.
Jul 30 '08 #1
7 1232
Thread Abort can be bad because there is no way of telling what state the
thread will be interrupted in. I'm guessing that calling Thread.Abort on a
one-way process where _none_ of the data it is modifying is shared (although
this can be difficult to guarantee) could be okay.

However generally speaking it's better to have background threads waiting on
some kind of boolean value which you then toggle when disposing/stopping the
thread. This means the threads reaches the end of it's method and just
returns.

e.g.

while(!isDisposed)
{
// perform background processing
}

// thread exits here

Furthermore your datagrid problem is definately _not_ related to threads per
se. This is because the UI in Winform apps in inherently single threaded.
Accessing a WinForm control on a background thread will cause an exception to
be thrown.

HTH

Simon

"Gidi" wrote:
Hi,

In my Windows application, I'm using thread, and I've 2 questions:

1. I read that it's not recommended to use Thread.Abort(), so what is the
best way to close the thread, in case the user wants to cancel or the thread
is finished and i want to close my application?

2. in my Thread I'm creating a dataGridView with DataTable as dataSoruce,
and I've problem with the scroll bars, I see the scroll bars but both of the
are frozen and i can't scroll between the lines, but if i'm using the middle
button in my mouse it does scroll. I took my code to another Win Form without
thread and i don't have this problem there, so my guess is that it's releated
to thread.
Does anyone have any idea?

Thanks,
Gidi.
Jul 30 '08 #2
Thanks Simon,

if my dataGridView problem doesn't releated to the thread, what can casue
this problem? and why it works fine on a regular win form and doesn't work
here?

Thanks,
Gidi

"Simon Tamman" wrote:
Thread Abort can be bad because there is no way of telling what state the
thread will be interrupted in. I'm guessing that calling Thread.Abort on a
one-way process where _none_ of the data it is modifying is shared (although
this can be difficult to guarantee) could be okay.

However generally speaking it's better to have background threads waiting on
some kind of boolean value which you then toggle when disposing/stopping the
thread. This means the threads reaches the end of it's method and just
returns.

e.g.

while(!isDisposed)
{
// perform background processing
}

// thread exits here

Furthermore your datagrid problem is definately _not_ related to threads per
se. This is because the UI in Winform apps in inherently single threaded.
Accessing a WinForm control on a background thread will cause an exception to
be thrown.

HTH

Simon

"Gidi" wrote:
Hi,

In my Windows application, I'm using thread, and I've 2 questions:

1. I read that it's not recommended to use Thread.Abort(), so what is the
best way to close the thread, in case the user wants to cancel or the thread
is finished and i want to close my application?

2. in my Thread I'm creating a dataGridView with DataTable as dataSoruce,
and I've problem with the scroll bars, I see the scroll bars but both of the
are frozen and i can't scroll between the lines, but if i'm using the middle
button in my mouse it does scroll. I took my code to another Win Form without
thread and i don't have this problem there, so my guess is that it's releated
to thread.
Does anyone have any idea?

Thanks,
Gidi.
Jul 30 '08 #3
On Jul 30, 11:43*am, Simon Tamman
<SimonTam...@discussions.microsoft.comwrote:

<snip>
Furthermore your datagrid problem is definately _not_ related to threads per
se. This is because the UI in Winform apps in inherently single threaded.
Accessing a WinForm control on a background thread will cause an exception to
be thrown.
That only happens under the debugger, by default. In release it will
just silently let you try to update the UI from the wrong thread.

Jon
Jul 30 '08 #4
It could be lots of things, which is why I haven't answered that question. I
would suggest that you extract the problem into a very small but complete
sample application and work out precisely exactly what is causing the issue.
I would expect that your threaded code is doing something else that the
other code isn't (it might be in the threads or maybe somewhere else
completely!). Perhaps the grids are Enabled = false? Just a guess.

"Gidi" wrote:
Thanks Simon,

if my dataGridView problem doesn't releated to the thread, what can casue
this problem? and why it works fine on a regular win form and doesn't work
here?

Thanks,
Gidi

"Simon Tamman" wrote:
Thread Abort can be bad because there is no way of telling what state the
thread will be interrupted in. I'm guessing that calling Thread.Abort on a
one-way process where _none_ of the data it is modifying is shared (although
this can be difficult to guarantee) could be okay.

However generally speaking it's better to have background threads waiting on
some kind of boolean value which you then toggle when disposing/stopping the
thread. This means the threads reaches the end of it's method and just
returns.

e.g.

while(!isDisposed)
{
// perform background processing
}

// thread exits here

Furthermore your datagrid problem is definately _not_ related to threads per
se. This is because the UI in Winform apps in inherently single threaded.
Accessing a WinForm control on a background thread will cause an exception to
be thrown.

HTH

Simon

"Gidi" wrote:
Hi,
>
In my Windows application, I'm using thread, and I've 2 questions:
>
1. I read that it's not recommended to use Thread.Abort(), so what is the
best way to close the thread, in case the user wants to cancel or the thread
is finished and i want to close my application?
>
2. in my Thread I'm creating a dataGridView with DataTable as dataSoruce,
and I've problem with the scroll bars, I see the scroll bars but both of the
are frozen and i can't scroll between the lines, but if i'm using the middle
button in my mouse it does scroll. I took my code to another Win Form without
thread and i don't have this problem there, so my guess is that it's releated
to thread.
Does anyone have any idea?
>
Thanks,
Gidi.
Jul 30 '08 #5
Really? oOo I never knew. Thanks Jon.
As an aside, where has your blog gone? All I see these days is a login
screen to mvp blogs.... or page not found. Has you killed it?

"Jon Skeet [C# MVP]" wrote:
On Jul 30, 11:43 am, Simon Tamman
<SimonTam...@discussions.microsoft.comwrote:

<snip>
Furthermore your datagrid problem is definately _not_ related to threads per
se. This is because the UI in Winform apps in inherently single threaded.
Accessing a WinForm control on a background thread will cause an exception to
be thrown.

That only happens under the debugger, by default. In release it will
just silently let you try to update the UI from the wrong thread.

Jon
Jul 30 '08 #6
Hi,

The problem was that i had to invoke the dataSource Binding,

I solved the problem with:

this.Invoke((MethodInvoker)delegate { summary_dgv.DataSource = summary_dt; });

thanks for the help.
"Jon Skeet [C# MVP]" wrote:
On Jul 30, 11:43 am, Simon Tamman
<SimonTam...@discussions.microsoft.comwrote:

<snip>
Furthermore your datagrid problem is definately _not_ related to threads per
se. This is because the UI in Winform apps in inherently single threaded.
Accessing a WinForm control on a background thread will cause an exception to
be thrown.

That only happens under the debugger, by default. In release it will
just silently let you try to update the UI from the wrong thread.

Jon
Jul 30 '08 #7
Simon Tamman <Si*********@discussions.microsoft.comwrote:
Really? oOo I never knew. Thanks Jon.
As an aside, where has your blog gone? All I see these days is a login
screen to mvp blogs.... or page not found. Has you killed it?
There was a problem with the upgrade to CS2008, but the blog is still
there. http://www.msmvps.com/jon.skeet will redirect you to
http://msmvps.com/blogs/jon_skeet/Default.aspx which is the "proper"
main page. If you take any entry URLs that have "jon.skeet" in and
change them to "jon_skeet", they should work.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jul 30 '08 #8

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

Similar topics

0
by: cottonviking | last post by:
Greetings, all! I'm pondering the pitfalls of aborting a secondary thread from the main thread in a service app that I'm working on in VBNET (fx 1.1). Everything I've read so far pretty much...
14
by: Daisy | last post by:
From this page: http://www.c-sharpcorner.com/2/mt_beginner1.asp Thread class's Abort method is called to kill a thread permanently. Make sure you call IsAlive before Abort. if (...
7
by: Morris | last post by:
I want to abort a running thread, so I call MyThread.abort() function. My problem is this thread runs "almost" like a while(true) loop and I don't want the Abort() function interrupts the thread at...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
18
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this...
1
by: Steve | last post by:
Hello, I have a subroutine, sub1 that creates 2 new Threads. Each Thread then runs a subroutine of their own. Sub1 is on a timer and when called will create 2 new threads each time. I...
6
by: Joe HM | last post by:
Hello - I have a function that calls Thread.Abort() to stop a thread in a _Closed() Method of a GUI. The thread contains a blocking call on a TCP socket and that is the easiest way to stop...
5
by: andrew | last post by:
Hi, I have the following issue with the Thread.Abort(): The main thread creates a worker thread which waits on a process termination. void ThreadProc() { Process proc =...
6
by: mehdi | last post by:
Hi folks, You know, the Thread class has got a method named Abort which according to the msdn: "Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.