Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

When thread routine returns, that means it was closed ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Boki - 09 Jul 2007 07:35 GMT
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.
Boki - 09 Jul 2007 07:45 GMT
> Hi All,
>
[quoted text clipped - 27 lines]
> 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.
Marc Gravell - 09 Jul 2007 08:21 GMT
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
Peter Duniho - 09 Jul 2007 18:04 GMT
> [...]
>                     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
Marc Gravell - 10 Jul 2007 05:00 GMT
> 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
Peter Duniho - 10 Jul 2007 09:51 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.