Console Application Stops From Another Task to Process.Start

Asked 2 years ago, Updated 2 years ago, 112 views

The .NET framework is 4.0.
If you run the following methods in the console application, Process.Start in Convert will stop processing (no exceptions will occur, but will not continue):
In addition, Process.Start has successfully invoked external applications, and the task manager has confirmed that the process is complete.
Invoking this method from the WindowsForm application will exit successfully.

public CancellationTokenSourceConvertAsync<T>(object obj,string[] files, Listener listener){
    var cancellation=new CancellationTokenSource();
    var task = new Task(
        ( ) = > {
             try{
                Task convertedTask = null;
                Action<object>action=new Action<object>(obj)=>{
                    try{                                                    
                        listener.oneFile(obj);
                        return;
                    } catch(Exception ex2){
                        Debug.WriteLine(ex2);
                        return;
                }});
                foreach(var file in files){

                    if(cancellation.IsCancellationRequested) return;
                    Debug.WriteLine("before:"+file);
                    var converted=Convert(obj,file); /*<- where Process.Start() call*/
                    Debug.WriteLine("after:"+converted);
                    if(converted==null)break;

                    if(convertedTask==null){
                        convertedTask = newTask(action,ab,cancellation.Token);
                        convertedTask.Start();
                    } else {
                        convertedTask=convertedTask.ContinueWith(arg)=>action(converted), cancellation.Token);
                    }
                }
                if(convertedTask!=null)convertedTask.Wait();
                listener.allDone();
            }catch(Exceptionex){
                Debug.WriteLine(ex);
                return;
            }

    },cancellation.Token);
    task.Start();
    return cancellation;
}

I would appreciate it if you could let me know the above.Thank you for your cooperation.

c# asynchronous

2022-09-30 21:25

1 Answers

The information was not disclosed, but the console application was waiting in Console.ReadLine() after calling ConvertAsync.
However, I changed the return value to Task as shown below, and after waiting at Task.Wait() on the console application side, it ended normally.The cause is still unknown.

public Task ConvertAsync<T>(object obj, string[] files, listener listener){
    var cancellation=new CancellationTokenSource();
    var task = new Task(
        ( ) = > {
             try{
                Task convertedTask = null;
                Action<object>action=new Action<object>(obj)=>{
                    try{                                                    
                        listener.oneFile(obj);
                        return;
                    } catch(Exception ex2){
                        Debug.WriteLine(ex2);
                        return;
                }});
                foreach(var file in files){

                    if(cancellation.IsCancellationRequested) return;
                    Debug.WriteLine("before:"+file);
                    var converted=Convert(obj,file); /*<- where Process.Start() call*/
                    Debug.WriteLine("after:"+converted);
                    if(converted==null)break;

                    if(convertedTask==null){
                        convertedTask = newTask(action, converted, cancellation.Token);
                        convertedTask.Start();
                    } else {
                        convertedTask=convertedTask.ContinueWith(arg)=>action(converted), cancellation.Token);
                    }
                }
                if(convertedTask!=null)convertedTask.Wait();
                listener.allDone();
            }catch(Exceptionex){
                Debug.WriteLine(ex);
                return;
            }

    },cancellation.Token);
    task.Start();
    return task;
}


2022-09-30 21:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.