473,503 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When thread routine returns, that means it was closed ?

Hi All,

When thread returns, that means the thread is closed ?
What is the standard way to kill it ?

threadA= null ;

Thank you for advice!
/* CODE BEGIN */

public partial class Form1 : Form
{
private static Thread thread_Interent;
...
public void Boki_internet_CallbackFunction()
{

{
// code a b c d e g g
Thread.Sleep(1000);

}
}
....
}
/* CODE END */
Best regards,
Boki.

Jul 9 '07 #1
5 1459
On Jul 9, 2:35 pm, Boki <bokit...@ms21.hinet.netwrote:
Hi All,

When thread returns, that means the thread is closed ?
What is the standard way to kill it ?

threadA= null ;

Thank you for advice!
/* CODE BEGIN */

public partial class Form1 : Form
{
private static Thread thread_Interent;
...

public void Boki_internet_CallbackFunction()
{

{
// code a b c d e g g
Thread.Sleep(1000);

}
}
...

}

/* CODE END */
Best regards,
Boki.
My issue is, my form is running, user needs to type some text in a
textbox, and the thread is downloading data from internet by this
line:

pictureBox1.Image = new System.Drawing.Bitmap(new
System.IO.MemoryStream(new
System.Net.WebClient().DownloadData(picStr)));

if there is no thread, program will be completely block until the
picture was loaded.

so, I only need this thread starting work when I need it to load a
picture.

Could you please advice any idea ?

Thanks a lot!

Best regards,
Boki.

Jul 9 '07 #2
How about:

pictureBox1.LoadAsync(url);

The best way of stopping a thread is to let it exit cleanly, possibly
getting it to short-circuit internally by toggling a flag or whatver
(to break from a loop). Setting a varible (to the thread) to null
doesn't impact the thread in the least.

In the more general sense, when working with forms you can use
pool-threads to perform background activities, e.g.

void StartLoadImage(string url) {
// runs on UI thread when method invoked
ThreadPool.QueueUserWorkItem(delegate {
// runs on background thread
using(WebClient webClient = new WebClient())
using(MemoryStream stream = new
MemoryStream(webClient.DownloadData(url))) {
this.Invoke((MethodInvoker)delegate { // not
BeginInvoke, else stream might Dispose()
// runs on UI thread when data is available
pictureBox1.Image = new Bitmap(stream);
});
}
});
}

You can also use async-callbacks, but I find the above easier to
read... but it doesn't take advantage of completion ports, etc...

Marc
Jul 9 '07 #3
On Mon, 09 Jul 2007 00:21:22 -0700, Marc Gravell <ma**********@gmail.com
wrote:
[...]
this.Invoke((MethodInvoker)delegate { // not
BeginInvoke, else stream might Dispose()
// runs on UI thread when data is available
pictureBox1.Image = new Bitmap(stream);
});
Maybe I'm missing something, but why not just create the Bitmap from the
Stream in the background delegate, instead of passing the Stream to the UI
delegate to be used to construct the Bitmap? Then you're not tied to
Invoke() versus BeginInvoke().

Pete
Jul 9 '07 #4
why not just create the Bitmap from the Stream in the background delegate

Looking at it again, I can't think of any reason not to... I guess it
didn't occur to me at the time (it was a hasty example, to show the
general case, given that LoadAsync exists).

For the OP's benefit, this would look like:

....
Bitmap bmp = new Bitmap(stream);
this.BeginInvoke((MethodInvoker) delegate {
// runs on UI thread when data is available
pictureBox1.Image = bmp;
}

Marc

Jul 10 '07 #5
On Mon, 09 Jul 2007 21:00:20 -0700, Marc Gravell <ma**********@gmail.com
wrote:
For the OP's benefit, this would look like:
Or (just to be complete):

....
Bitmap bmp = new Bitmap(stream);
this.Invoke((MethodInvoker) delegate {
// runs on UI thread when data is available
pictureBox1.Image = bmp;
}

:)

Pete
Jul 10 '07 #6

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

Similar topics

1
271
by: Greg | last post by:
I am teaching myself visual basic .net 2003. I bought a book and worked the examples, but I cannot figure out what I am doing wrong on my first application. I have a form that is called from a...
4
7783
by: bill | last post by:
I am confused by the behavior of the following code. The function copy() takes two FILE *'s and copies the data from one to the other. In the main routine, I create a pipe and copy stdin to the...
9
4887
by: tshad | last post by:
This is from my previous post, but a different issue. I have the following Javascript routine that opens a popup page, but doesn't seem to work if called from an asp.net button. It seems to work...
6
1899
by: henk | last post by:
Hi, I want to show a message in my vb.net program when another windows program (eg calc.exe) is stopped by a user. with pList = myProcess.GetProcesses For Each myProcess In pList I can check...
2
3000
by: Tom | last post by:
How is the best way to avoid validation when closing a window? For instance, I have a Windows Forms window which has a validation event for a text box. However, if one enters invalid data in then...
21
18113
by: alistair_henderson | last post by:
Morning All, I have some code for a website which uses 'window.open' to simulate modal dialog boxes. I use the window.closed property to decide if the window object exists at various points. ...
22
3976
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to...
13
7330
by: Edwin Smith | last post by:
I have a form which displays a DataGridView table generated with the VS2005 tools. The database is a Pervasive v.9 with an ODBC driver. The DataGridView works great except when I'm done and I...
14
2086
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I tried a google search but could not find anything. I am trying to cause one webpage to reload when a second web page is closed. The second webpage loads data into a session variable and when...
0
7201
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
7278
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
7328
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
7456
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
5578
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,...
1
5011
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
3166
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...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.