Click here to Skip to main content
15,887,746 members
Home / Discussions / C#
   

C#

 
GeneralOpinions needed on Win Form Design Pin
MrJJKoolJ11-Feb-04 11:29
MrJJKoolJ11-Feb-04 11:29 
GeneralRe: Opinions needed on Win Form Design Pin
Heath Stewart11-Feb-04 12:15
protectorHeath Stewart11-Feb-04 12:15 
GeneralRe: Opinions needed on Win Form Design Pin
MrJJKoolJ11-Feb-04 14:18
MrJJKoolJ11-Feb-04 14:18 
GeneralRe: Opinions needed on Win Form Design Pin
Heath Stewart12-Feb-04 3:26
protectorHeath Stewart12-Feb-04 3:26 
GeneralSetting Properties through .Net Remoting Pin
Brian Rogan11-Feb-04 11:09
Brian Rogan11-Feb-04 11:09 
Generalproblem with XmlDocument Pin
visiontec11-Feb-04 10:50
visiontec11-Feb-04 10:50 
GeneralRe: problem with XmlDocument Pin
Kentamanos11-Feb-04 11:01
Kentamanos11-Feb-04 11:01 
QuestionIs anything wrong with this? Pin
profoundwhispers11-Feb-04 10:40
profoundwhispers11-Feb-04 10:40 
<br />
  using System;<br />
  using System.Threading;<br />
<br />
  namespace ConsoleApplication<br />
  {<br />
  class Page<br />
    {<br />
       private int _page;<br />
<br />
       public Page()<br />
       {<br />
         _page = 0;<br />
       }<br />
<br />
       public int CurrentPage<br />
       {<br />
         get<br />
         {<br />
            lock(this)<br />
            {<br />
              _page++;<br />
              return _page;<br />
            }<br />
         }<br />
       }<br />
     }<br />
<br />
     class Class1<br />
     {<br />
       static bool stop = false;<br />
       static Page page = new Page();<br />
<br />
       [STAThread]<br />
       static void Main(string[] args)<br />
       {<br />
         Thread[] threads = new Thread[5];<br />
<br />
         for (int i = 0; i < threads.Length; i++)<br />
         {<br />
           threads[i] = new Thread(new ThreadStart(ThreadProc));<br />
           threads[i].Name = Convert.ToString(i);<br />
           threads[i].Start();<br />
         }<br />
         Console.ReadLine();<br />
       }<br />
<br />
       static void ThreadProc()<br />
       {<br />
         int i;<br />
         while (!stop)<br />
         {<br />
           i = page.CurrentPage;<br />
           Console.WriteLine("Thread {1}: Current page number: {0}", i, Thread.CurrentThread.Name);<br />
           Console.WriteLine("Thread {1}: Downloading page {0}", i, Thread.CurrentThread.Name);<br />
           Console.WriteLine("Thread {1}: Processing page {0}", i, Thread.CurrentThread.Name);<br />
           if (i == 10) stop = true;<br />
         }<br />
       }<br />
     }<br />
  }<br />


Of course instead of the Console.WriteLine statements, there will actually be functions that perform the action.

What I'm worried about is the following: Since each thread is working with the same ThreadProc, isn't this at all going to affect the local variable (integer) i that refers to the page?

When I actually ran this code, I didn't get the statements to execute in order. Is that okay under the code circumstances? Again, what I mean is when one thread is executing, say downloading, it should then start processing the downloaded page. If that doesn't happen, and another thread, instead, starts downloading it's own page, wouldn't that affect the page processing of the first thread? I guess this is just another method of asking the same question: is anything going to happen to the local variable i in between thread calls?

Am I doing the right thing here, or should I use another method to do what I'm trying to do: Download 5 pages at a time. For each downloaded page: process it, and store information. When reaching 100 pages, stop.

Sammy

"A good friend, is like a good book: the inside is better than the cover..."
AnswerRe: Is anything wrong with this? Pin
Heath Stewart11-Feb-04 12:08
protectorHeath Stewart11-Feb-04 12:08 
GeneralDatabinding to combobox Pin
Anonymous11-Feb-04 10:34
Anonymous11-Feb-04 10:34 
GeneralRe: Databinding to combobox Pin
Heath Stewart11-Feb-04 12:00
protectorHeath Stewart11-Feb-04 12:00 
Generalmanaging Deletes and Backspaces Pin
obelisk2911-Feb-04 10:19
obelisk2911-Feb-04 10:19 
GeneralRe: managing Deletes and Backspaces Pin
Heath Stewart11-Feb-04 11:57
protectorHeath Stewart11-Feb-04 11:57 
Generalmanaged c++ class that inherits from unmanaged c++ Pin
godzooky11-Feb-04 8:30
godzooky11-Feb-04 8:30 
GeneralRe: managed c++ class that inherits from unmanaged c++ Pin
Heath Stewart11-Feb-04 9:47
protectorHeath Stewart11-Feb-04 9:47 
GeneralRe: managed c++ class that inherits from unmanaged c++ Pin
godzooky11-Feb-04 9:50
godzooky11-Feb-04 9:50 
GeneralRe: managed c++ class that inherits from unmanaged c++ Pin
Heath Stewart11-Feb-04 9:52
protectorHeath Stewart11-Feb-04 9:52 
GeneralRe: managed c++ class that inherits from unmanaged c++ Pin
godzooky11-Feb-04 10:07
godzooky11-Feb-04 10:07 
GeneralRe: managed c++ class that inherits from unmanaged c++ Pin
Heath Stewart11-Feb-04 10:10
protectorHeath Stewart11-Feb-04 10:10 
GeneralMouse Button &gt; keystroke Pin
Spanky311-Feb-04 8:04
Spanky311-Feb-04 8:04 
GeneralRe: Mouse Button &gt; keystroke Pin
Kentamanos11-Feb-04 8:15
Kentamanos11-Feb-04 8:15 
GeneralRe: Mouse Button &gt; keystroke Pin
Daniel Turini11-Feb-04 8:16
Daniel Turini11-Feb-04 8:16 
GeneralRe: Mouse Button &gt; keystroke Pin
Spanky311-Feb-04 8:28
Spanky311-Feb-04 8:28 
GeneralRe: Mouse Button &gt; keystroke Pin
Kentamanos11-Feb-04 9:20
Kentamanos11-Feb-04 9:20 
GeneralRe: Mouse Button &gt; keystroke Pin
Anonymous11-Feb-04 9:31
Anonymous11-Feb-04 9:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.