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

C#

 
QuestionShare point problem Pin
manju#12330-Jul-08 23:45
manju#12330-Jul-08 23:45 
GeneralRe: Share point problem Pin
Simon P Stevens31-Jul-08 0:06
Simon P Stevens31-Jul-08 0:06 
GeneralRe: Share point problem Pin
manju#12331-Jul-08 0:31
manju#12331-Jul-08 0:31 
QuestionRecord Audio from Internet Explorer Pin
Malenko30-Jul-08 23:28
Malenko30-Jul-08 23:28 
QuestionA quick question about volatile Pin
Megidolaon30-Jul-08 23:27
Megidolaon30-Jul-08 23:27 
AnswerRe: A quick question about volatile Pin
Guffa30-Jul-08 23:44
Guffa30-Jul-08 23:44 
AnswerRe: A quick question about volatile Pin
Mike Dimmick31-Jul-08 0:13
Mike Dimmick31-Jul-08 0:13 
AnswerRe: A quick question about volatile Pin
Megidolaon31-Jul-08 1:11
Megidolaon31-Jul-08 1:11 
Thanks a lot, that was quite informative.
The main reason I made this topic is because I read aborting a thread by periodically checking a volatile bool is a good way to do it.
In the case of a bool, is it truly reliable that by the time the value is read, it is up to date?
Also, is there a better way to check for the cancellation request other than wrapping each 5-10 operations in an if statement?

And now I have a whole new problem, I was testing around with the lock keyword and noticed something I don't like at all.
class Program
{
    //global variable accessible by all threads
    static List<string> list = new List<string>();
     
    static void Main(string[] args)
    {
        Thread t1 = new Thread(new ThreadStart(DoSomething));
        Thread t2 = new Thread(new ThreadStart(DoSomethingElse));

        //start thread t2, then t1
        t2.Start();
        t1.Start();
        Console.ReadLine();
    }

   //Thread 1's work
    static private void DoSomething()
    {
            //wait until thread 2 finished locking
            Thread.Sleep(13);
            for (int i = 0; i < 100; i++)
            {
                //first add "1" to the list and then write it to the console
                list.Add("1");
                Console.WriteLine("1");
                //checks if this thread accesses the list before t2 finished its work
                if (list.Count< 100 && list.Contains("2"))
                {
                    Console.WriteLine("FIRST LOCK BREACHED!");
                    break;
                }
            }
           //writes thread number and count to the console
           Console.WriteLine("1-Count: " + list.Count.ToString());
    }

   //Thread 2's work 
   static private void DoSomethingElse()
   {
       lock (list)
       {
           for (int i = 0; i < 100; i++)
           {
               //first add "2" to the list and then write it to the console
               list.Add("2");
               Console.WriteLine("2");
                
               //checks if thread t accessed the list before the lock was released
               if (list.Contains("1"))
               {
                   Console.WriteLine("SECOND LOCK BREACHED!");
                   break;
               }
           }
           //writes thread number and count to the console
           Console.WriteLine("2-Count: " + list.Count.ToString());    
       }
 }</string></string>
This will produce random output, it seems the threads get different time slices every time I execute the program, how come?

Also, sometimes it will produce this output:
...
2
1
FIRST LOCK BREACHED
1-Count: 19
SECOND LOCK BREACHED
2-Count:19
This is confusing me, because I thought lock actually blocks other threads. However that is apparently not the case.
Thread 1 always starts after thread 2 and since thread 2 locks "list", thread 1 should have to wait until thread 2 is done, but apparently it slips in somewhere in between and still accesses the list.
By the time thread 2 writes to the list, it is empty but 3 lines below, when it checks if the list contains a "1", it really does. Why does that happen?

And I tried the same with wrapping all of thread 1's work in a lock of "list", in that case everything will be executed in order, no matter how many times I test it.

I'd really appreciate if you could clear this up.
Thank in advance.
GeneralRe: A quick question about volatile Pin
Mike Dimmick31-Jul-08 1:32
Mike Dimmick31-Jul-08 1:32 
GeneralRe: A quick question about volatile Pin
Guffa31-Jul-08 1:37
Guffa31-Jul-08 1:37 
AnswerRe: A quick question about volatile Pin
Megidolaon31-Jul-08 1:59
Megidolaon31-Jul-08 1:59 
QuestionHow to insert a new row to MDB file [modified] Pin
Admin88730-Jul-08 21:53
Admin88730-Jul-08 21:53 
AnswerRe: How to insert a new row to MDB file Pin
leppie30-Jul-08 22:41
leppie30-Jul-08 22:41 
AnswerRe: How to insert a new row to MDB file Pin
rah_sin30-Jul-08 22:42
professionalrah_sin30-Jul-08 22:42 
QuestionSetup with Custom Action Pin
Sifar - 030-Jul-08 21:45
Sifar - 030-Jul-08 21:45 
AnswerRe: Setup with Custom Action Pin
leppie30-Jul-08 22:43
leppie30-Jul-08 22:43 
AnswerRe: Setup with Custom Action Pin
Frank Horn30-Jul-08 23:17
Frank Horn30-Jul-08 23:17 
Questionproblem_details to solve the warning Pin
ktamanna30-Jul-08 21:42
ktamanna30-Jul-08 21:42 
AnswerRe: problem_details to solve the warning Pin
leppie30-Jul-08 22:44
leppie30-Jul-08 22:44 
QuestionHow to resize the form at run time on load Pin
tasumisra30-Jul-08 21:32
tasumisra30-Jul-08 21:32 
AnswerRe: How to resize the form at run time on load Pin
Syed Shahid Hussain30-Jul-08 22:32
Syed Shahid Hussain30-Jul-08 22:32 
GeneralRe: How to resize the form at run time on load Pin
tasumisra30-Jul-08 23:04
tasumisra30-Jul-08 23:04 
GeneralRe: How to resize the form at run time on load Pin
Syed Shahid Hussain31-Jul-08 17:39
Syed Shahid Hussain31-Jul-08 17:39 
GeneralRe: How to resize the form at run time on load Pin
tasumisra31-Jul-08 23:36
tasumisra31-Jul-08 23:36 
GeneralRe: How to resize the form at run time on load Pin
tasumisra31-Jul-08 23:39
tasumisra31-Jul-08 23:39 

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.