473,669 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1242
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(!isDispos ed)
{
// 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(!isDispos ed)
{
// 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...@di scussions.micro soft.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(!isDispos ed)
{
// 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...@di scussions.micro soft.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((Me thodInvoker)del egate { summary_dgv.Dat aSource = summary_dt; });

thanks for the help.
"Jon Skeet [C# MVP]" wrote:
On Jul 30, 11:43 am, Simon Tamman
<SimonTam...@di scussions.micro soft.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*********@di scussions.micro soft.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.co m>
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
255
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 dissuades one against aborting one thread from another, and I'm almost ready to acquiesce but curiosity leads me on. Below is a procedure template that I wonder would guarantee behavior even in the event that a ThreadAbortException occurred. ...
14
5346
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 ( thread.IsAlive ) { thread.Abort(); }
7
3279
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 any point in the thread. In fact, I have a section of code needs to be "protected" from being interrupted. How can I make sure Abort() will not land anywhere winthin this block? In other words, the Abort() must wait until this block of code is done...
20
3012
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 delegate inside the UI that I call to update the progress meter. I use the Suspend() and Abort() methods based on button events. I can watch the progress meter increase just fine when the thread is running. When I select Start, I enable the Cancel...
18
5846
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 component becomes instabile, throwing a Windows like error (access violation on 0x00000002), not an framework exception. The component and all of its subcomponents are 100% managed code. What could go wrong with Thread.Abort()? Thanks for any hints.
1
1261
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 observed the Windows Task Manager Performance Tab for the count of threads, handles, processes. I let Sub1 run several times and create several threads. I thought of terminating each thread with Thread.Abort. Task Manager does show an increase in...
6
5465
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 that. This thread is also outputting strings in a RichTextBox and in some rare instances I get a System.NullReferenceException when I exit the GUI. It seems like the _Closed() Method calls Thread.Abort() and then continues closing down/disposing...
5
5071
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 = proc.Start("notepad.exe");
6
2934
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 terminating the thread. Calling this method usually terminates the thread." I've had a long discussion with someone on not to use the mentioned method unless under the most extreme cases. I believe that it's
0
8465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8383
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8803
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8658
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7407
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6210
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.