Click here to Skip to main content
15,895,084 members
Home / Discussions / C#
   

C#

 
QuestionHooking Direct Api Pin
danzar5-Nov-07 13:16
danzar5-Nov-07 13:16 
QuestionMaking Project Pin
Sameh96140335-Nov-07 11:30
Sameh96140335-Nov-07 11:30 
AnswerRe: Making Project Pin
Paul Conrad5-Nov-07 15:25
professionalPaul Conrad5-Nov-07 15:25 
QuestionData not stored on pocket pc Pin
karanjsingh5-Nov-07 11:19
karanjsingh5-Nov-07 11:19 
QuestionThreadsafe "for" without lock() Pin
szolDat5-Nov-07 11:05
szolDat5-Nov-07 11:05 
AnswerRe: Threadsafe "for" without lock() Pin
Guffa5-Nov-07 12:07
Guffa5-Nov-07 12:07 
AnswerRe: Threadsafe "for" without lock() Pin
szolDat5-Nov-07 12:31
szolDat5-Nov-07 12:31 
GeneralRe: Threadsafe "for" without lock() Pin
PIEBALDconsult5-Nov-07 16:02
mvePIEBALDconsult5-Nov-07 16:02 
Have you tried it the other way? Or just believed some rubbish that was floating around the Internet? Big Grin | :-D

Because your i is defined within the method, each invocation will have its own i, not one shared one. Thread safety comes into effect with fields -- items that are defined in a class or struct.

Here, I whipped up a little (!) demo:

C#
namespace Template
{
    public partial class Template
    {
        private int[] values ;
        private int   classi ;
 
        public Template
        (
            int HowMany
        )
        {
            this.values = new int [ HowMany ] ;
 
            for ( int i = 0 ; i < HowMany ; i++ )
            {
                this.values [ i ] = i ;
            }
 
            return ;
        }
 
        public void
        PrintLocal
        (
            object Format
        )
        {
            for ( int locali = 0 ; locali < this.values.Length ; locali++ )
            {
                System.Console.Write
                (
                    (string) Format
                ,
                    this.values [ locali ]
                ) ;
            }
 
            return ;
        }
 
        public void
        PrintClass
        (
            object Format
        )
        {
            for ( this.classi = 0 ; this.classi < this.values.Length ; this.classi++ )
            {
                System.Console.Write
                (
                    (string) Format
                ,
                    this.values [ this.classi ]
                ) ;
            }
 
            return ;
        }
 
        public void
        PrintClassWithLock
        (
            object Format
        )
        {
            lock ( this.values )
            {
                for ( this.classi = 0 ; this.classi < this.values.Length ; this.classi++ )
                {
                    System.Console.Write
                    (
                        (string) Format
                    ,
                        this.values [ this.classi ]
                    ) ;
                }
            }
            
            return ;
        }
 
        [System.STAThreadAttribute]
        public static int
        Main
        (
            string[] args
        )
        {
            int result = 0 ;
 
            Template t = new Template ( 100 ) ;
 
            try
            {
                System.Threading.Thread a = new System.Threading.Thread ( t.PrintLocal ) ;
                System.Threading.Thread b = new System.Threading.Thread ( t.PrintLocal ) ;
 
                a.Start ( " A({0:00})" ) ;
                b.Start ( " B({0:00})" ) ;
 
                while ( a.IsAlive && b.IsAlive )
                {
                    System.Threading.Thread.Sleep ( 100 ) ;
                }
         
                System.Threading.Thread c = new System.Threading.Thread ( t.PrintClass ) ;
                System.Threading.Thread d = new System.Threading.Thread ( t.PrintClass ) ;
 
                System.Console.WriteLine() ;
                System.Console.WriteLine() ;
                
                c.Start ( " C({0:00})" ) ;
                d.Start ( " D({0:00})" ) ;
 
                while ( c.IsAlive && d.IsAlive )
                {
                    System.Threading.Thread.Sleep ( 100 ) ;
                }
         
                System.Threading.Thread e = new System.Threading.Thread ( t.PrintClassWithLock ) ;
                System.Threading.Thread f = new System.Threading.Thread ( t.PrintClassWithLock ) ;
 
                System.Console.WriteLine() ;
                System.Console.WriteLine() ;
                
                e.Start ( " E({0:00})" ) ;
                f.Start ( " F({0:00})" ) ;
 
                while ( e.IsAlive && f.IsAlive )
                {
                    System.Threading.Thread.Sleep ( 100 ) ;
                }
            }
            catch ( System.Exception err )
            {
                System.Console.Write ( err.Message ) ;
            }
 
            return ( result ) ;
        }
    }
}

AnswerRe: Threadsafe &amp;quot;for&amp;quot; without lock() Pin
Guffa5-Nov-07 20:24
Guffa5-Nov-07 20:24 
GeneralRe: Threadsafe &amp;quot;for&amp;quot; without lock() Pin
szolDat6-Nov-07 21:38
szolDat6-Nov-07 21:38 
AnswerRe: Threadsafe &amp;quot;for&amp;quot; without lock() Pin
Guffa6-Nov-07 22:20
Guffa6-Nov-07 22:20 
Questionunknown problem is visual studio Pin
netJP12L5-Nov-07 10:38
netJP12L5-Nov-07 10:38 
AnswerRe: unknown problem is visual studio Pin
Colin Angus Mackay5-Nov-07 11:00
Colin Angus Mackay5-Nov-07 11:00 
GeneralRe: unknown problem is visual studio Pin
netJP12L6-Nov-07 3:56
netJP12L6-Nov-07 3:56 
GeneralRe: EventArgs Pin
half-life5-Nov-07 10:34
half-life5-Nov-07 10:34 
QuestionHash Encryption Pin
Eli Nurman5-Nov-07 9:15
Eli Nurman5-Nov-07 9:15 
AnswerRe: Hash Encryption Pin
led mike5-Nov-07 9:22
led mike5-Nov-07 9:22 
GeneralRe: Hash Encryption Pin
Anthony Mushrow5-Nov-07 12:25
professionalAnthony Mushrow5-Nov-07 12:25 
AnswerRe: Hash Encryption Pin
dino20945-Nov-07 9:42
dino20945-Nov-07 9:42 
QuestionTouchScreen And Mouse separate. [modified] Pin
anselmo5-Nov-07 9:06
anselmo5-Nov-07 9:06 
QuestionQuery based on GUID in Access DB Pin
Christian Graus5-Nov-07 8:58
protectorChristian Graus5-Nov-07 8:58 
AnswerRe: Query based on GUID in Access DB Pin
pmarfleet5-Nov-07 9:51
pmarfleet5-Nov-07 9:51 
GeneralRe: Query based on GUID in Access DB [modified] Pin
Christian Graus5-Nov-07 9:59
protectorChristian Graus5-Nov-07 9:59 
GeneralRe: Query based on GUID in Access DB Pin
pmarfleet5-Nov-07 10:16
pmarfleet5-Nov-07 10:16 
GeneralRe: Query based on GUID in Access DB Pin
Christian Graus5-Nov-07 10:31
protectorChristian Graus5-Nov-07 10: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.