Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a pause in a procedure and then return value after it?

Tags:

c#

pause

I'm working on a C# project, want to make a small pause about 2 seconds inside a procedure.

Actually I have tried to use Invoke, but as you know, we can't use it inside a class this kind of procedure.

Here is my code for more details:

public class GenerateFile
{

    public CSPFEnumration.ProcedureResult GenerateFaxFile(string Daftar_No, string Channelno, string NationalCode)
    {
        string script = string.Format(" DECLARE @RC INT " +
                                        " DECLARE @Daftar_No INT = '{0}' " +
                                        " DECLARE @hokm_type_code INT = 100 " +
                                        " DECLARE @Channelno INT = '{1}' " +
                                        " DECLARE @Id_No BIGINT = '{2}' " +
                                        " EXEC @rc = [dbo].[Hokm_with_type] @Daftar_No, @hokm_type_code, @Channelno, @Id_No ",
                                        Daftar_No,
                                        Channelno,
                                        NationalCode);
        try
        {
            IEnumerable<string> commandStrings = Regex.Split(script, @"^\s*GO\s*$",
                                                    RegexOptions.Multiline | RegexOptions.IgnoreCase);
            Connect();
            foreach (string commandString in commandStrings)
            {
                if (commandString.Trim() != "")
                {
                    using (var command = new SqlCommand(commandString, Connection))
                    {
                        command.ExecuteNonQuery();
                    }
                }
            }

            DisConnect();

            string FaxFilePath = InternalConstant.FaxFilePath + "\\" + string.Format("Lhokm{0}.tif", Channelno);


            // I want to make a pause in here without locking UI

            if (File.Exists(FaxFilePath))
                return CSPFEnumration.ProcedureResult.Success;
            else
                return CSPFEnumration.ProcedureResult.Error;

        }
        catch (Exception ex)
        {
            InternalDatabase.GetInstance.InsertToPensionOrganizationException(ex);
            return CSPFEnumration.ProcedureResult.Error;
        }
    }
}

I have tried await too, but I cant return a proper value. because in this procedure if I use await, the value will return before finishing await.

Edit:

And also I dont want to use Thread.Sleep because it will lock UI. Thanks for any helping.

like image 276
Elahe Avatar asked Oct 08 '15 05:10

Elahe


3 Answers

Use async await feature :

Mark your method as async .

Add Task.Delay(2000) as the waited task.

 public async CSPFEnumration.ProcedureResult GenerateFaxFile(string Daftar_No, string Channelno, string NationalCode)
        {
                -----
                // I want to make a pause in here without locking UI
              await Task.Delay(2000);
                -----
        }
like image 159
Igoy Avatar answered Oct 06 '22 08:10

Igoy


Asking for downvotes:

DoEvents

Warning: Total, Complete and Inexcusably Flagrant Barnyard Programming:

// before call (disable the UI element that called this so it can't re-enter)
DateTime st = DateTime.Now();
while(DateTime.Now.Subtract(st).TotalSeconds<3)
    System.Windows.Forms.DoEvents();
// after call (re-enable UI element)

This will appear to work. No responsibility if people point and laugh.

Hey, you asked!

like image 30
FastAl Avatar answered Oct 06 '22 10:10

FastAl


You can look around Task.Delay() it will not block current thread and continue execution after number of milliseconds.

Exmaple usage from msdn:

Stopwatch sw = Stopwatch.StartNew();
var delay = Task.Delay(1000).ContinueWith(_ =>
                           { sw.Stop();
                             return sw.ElapsedMilliseconds; } );

Console.WriteLine("Elapsed milliseconds: {0}", delay.Result);
// The example displays output like the following:
//        Elapsed milliseconds: 1013

Or maybe look around Timer class.

like image 31
Mikhail Tulubaev Avatar answered Oct 06 '22 09:10

Mikhail Tulubaev