1

I've got a fairly simple program to enter and serialize an object using a lambda expression to pass things off to another thread.

using System;
using System.Threading;

using Newtonsoft.Json;

namespace MultithreadingApplication
{
    class ThreadCreationProgram
    {
        static void Main(string[] args)
        {
            myObject theObject = new myObject();
            Console.WriteLine("Enter the following:");
            Console.WriteLine("color:");
            theObject.Color = Console.ReadLine();
            Console.WriteLine("number");
            theObject.Color = Console.ReadLine();
            Console.WriteLine("shape:");
            theObject.Shape = Console.ReadLine();

            Thread myNewThread = new Thread(() => Serialize(theObject));
            myNewThread.Start();            
            myNewThread.Abort();            
            Console.ReadKey();
        }

        public static void Serialize(myObject theObject)
        {
            string json = JsonConvert.SerializeObject(theObject, Formatting.Indented);
            Console.WriteLine(json);
            Thread.Sleep(1000);
        }
    }

    public class myObject
    {
        private Int32 number;
        private String color, shape;

        public Int32 Number
        {
            get { return number; }
            set { number = value; }
        }

        public String Color
        {
            get { return color; }
            set { color = value; }
        }

        public String Shape
        {
            get { return shape; }
            set { shape = value; }
        }

        public myObject()
        {

        }
    }
}

When I run this thing, I notice that sometimes, it won't actually call the Serialize method. I've examined it with breakpoints and it will instantiate the thread with the lambda expression statement and immediately terminate it without ever going down to the Serialize method. I'm new to multithreading, so what's the deal here?

nerdenator
  • 1,265
  • 2
  • 18
  • 35
  • 2
    You're calling `Start()`... then immediately calling `Abort()`... – jebar8 Jun 27 '14 at 22:10
  • Perhaps I'm unclear, isn't start supposed to run the method I assign it? – nerdenator Jun 27 '14 at 22:11
  • And `Abort()` aborts the thread which may not have finished running anything yet. – Preston Guillot Jun 27 '14 at 22:15
  • 3
    This question appears to be off-topic because it is asking why something happens when the code says "start doing something" and then immediately says "stop doing whatever it is you're doing", and then asking why the code seems to be doing nothing at times. – Lasse V. Karlsen Jun 27 '14 at 22:19

2 Answers2

4
myNewThread.Start();            
myNewThread.Abort();

The thread sometimes fails to make any progress because you abort it before it has a chance to execute. If you want the thread to execute don't abort it.

The whole point of threads is that they execute independent of each other. When you call Start the thread is instructed to begin executing. In the meantime the calling thread is free to continue. In your case it immediately aborts the thread. And this can happen before the thread has got going.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Perhaps I'm not understanding just how this works. I got the basis of my code [here](http://www.tutorialspoint.com/csharp/csharp_multithreading.htm). I was under the impression that Abort would only call once the task had been completed, I assume this is wrong? – nerdenator Jun 27 '14 at 22:18
  • Abort is called when you call it. If you want to wait until the thread is done, wait on it. At which point you don't need to call Abort. – David Heffernan Jun 27 '14 at 22:19
  • Ah, okay, that makes sense. I'll fix that then. Thanks! – nerdenator Jun 27 '14 at 22:21
0

The problem here is that you call Thread.Start() which tells the computer: "Hey, begin doing this work in the lamda expression in a different context.

Right when that message is sent, you are immediately calling Thread.Abort() [http://msdn.microsoft.com/en-us/library/System.Threading.Thread_methods%28v=vs.110%29.aspx ]. It immediately kills the thread so sometimes no work will get done at all.

This SO answer should point you in the right direction: How to wait for thread to finish with .NET?

Checkout Thread.Join() here

Community
  • 1
  • 1
Daniel Kotin
  • 326
  • 2
  • 8