-1

Here I can't able to check the if condition (data) value from thread method. it's directly return the else part. based on the thread value I need to check the conditions. how can I achieve please help me.

public ActionResult Registration(VMAccount vm)
        {
            try
            {
                var existUser = accountBusiness.GetPhoneData(vm.Mobile);
                
                if (existUser.Count == 0)
                {
                    string data = "";
                    //vm.CreatedBy = GetAdminID();
                    //vm.ModifiedBy = vm.CreatedBy;
                    vm.Role = AppRole.Consumer;
                    int retValue = accountBusiness.ConsumerRegistration(vm);
                    if (retValue == 1)
                    {
                        new Thread(() =>
                        {
                            // string mobile = "1235896325";
                            data = sms.SendRegistrationOtp(vm.Mobile);
                            //data= sms.SendRegistrationOtp(mobile); 

                        }).Start();
                    }

                     if(data=="Pending")
                    {
                        return Json(2, JsonRequestBehavior.AllowGet);

                    }
                    else
                    {
                        return Json(3, JsonRequestBehavior.AllowGet);

                    }
                    
                }
                else
                {
                    return Json(1, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                log.Error("Admin-InsertAccountDetails", ex);
                return Json(0, JsonRequestBehavior.AllowGet);
            }
        }
MD. RAKIB HASAN
  • 3,670
  • 4
  • 22
  • 35
  • You should wait until thread complete before go to `if-else`. There is some ways how to do it (https://stackoverflow.com/questions/1584062/how-can-i-wait-for-a-thread-to-finish-with-net), but I think your `sms.SendRegistrationOtp(...)` should be awaitable `Task` to use as `data = await sms.SendRegistrationOtp(vm.Mobile);` without creating new Threads. – Auditive Oct 22 '21 at 10:16
  • Why do you need a new thread to handle `sms` calls? @Auditive looking at the controller action, it is not asynchronous. So one cannot just simply `await` inside it. – Dandry Oct 22 '21 at 10:18
  • When you call `Thread.Start()` - it starts, but code didn't wait for it completion and goes forward. So your `data` still empty string when it comes to `if-else`. And, because of it, you always get into `else` case. – Auditive Oct 22 '21 at 10:18
  • @Dandry I mean that `Thread` creating isn't efficient way. Even if you don't have `async` keyword at method - you can use `Task`s and wait for it completions. Check this fiddle as example: https://dotnetfiddle.net/MQ2gGw. And why it can't be `public async Task Registration()` in ASP.NET to use `await` inside? – Auditive Oct 22 '21 at 11:03
  • @Auditive framework boundaries for example. As far as I know, ASP.NET MVC 4 added support for async controller actions. I am not saying this is the case for the OP. – Dandry Oct 22 '21 at 11:09

1 Answers1

-1

I solved the issue

step 1: i created object for thread Thread process = new Thread(() =>{thread method});

step 2: process.start(); process.join();

Now i can able to return the thread value.

  • It entirely defeats the purpose of creating the thread if you're just going to synchronously block on it. Just run the method the thread would have run directly. – Servy Oct 22 '21 at 13:51