-2

I am working on wpf application where so many UI screens are available and each UI contains lots of processing using multi threading, task await, dispatcher and, background worker. So now I have two buttons START and STOP. Now when I will click START button then whole application's process(long running process) need to run in while loop. Which causing application freeze. So now I want to transfer my process in another class in another normal application. which will contains only logic and I will need to send only single parameter to that application.

Now I want to achieve like when I will click on start button from my WPF application then another application(which contains only cs file there will be not any type of UI) need to run in while loop. And again when I will click STOP button then again that application need to stop it's work. I don't know about it's logic how to implement but I have an idea like I describe. So please can you guide me little bit that how can I implement that ?

For that like I have a WPF application and second one is console application. Now in my Console Application code is like :

public static void Main(string[] args)
{
      startProcess("This is running");
}

public static void startProcess(string name)
{
     StreamWriter log;
     string filePath = @"D:\TimeLogFile.txt";

     for (int i = 0; i <= 10; i++)
     {
         log = File.AppendText(filePath);
         log.WriteLine(name);
         log.Close();
     }
}

now I need to pass string parameter from my WPF application and want to run this console application from my WPF application. So please can you guide me that how can I achive it ?

from WPF application when I will click Start button then I want to run console application with passing parameters. I have try below code which is running my console application but don't know how to pass parameter and fetch it on console application side.

 using(System.Diagnostics.Process process = new System.Diagnostics.Process())
{
       process.StartInfo.UseShellExecute = false;
       process.StartInfo.FileName = @"D:\StockTest.exe";
       process.StartInfo.CreateNoWindow = true;
       process.Start();
}

it's running console application but I want to pass parameter also there. So how can I pass parameter to console application and run console application.

5
  • Whether you are displaying anything to UI from that long running task , while the operation is going on ? Commented Dec 6, 2019 at 4:16
  • no I am not displaying on UI side. But still it's freezing. So I need to separate my code from my UI portion. Commented Dec 6, 2019 at 4:17
  • Do you really need a separate process? Why can't you do the work inside a GUI application (on a separate thread)? Commented Dec 6, 2019 at 5:24
  • The question is not relevant to #multithreading, #task or #scheduled-tasks any more. You just want to pass arguments to another process. Commented Dec 6, 2019 at 7:41
  • Sounds to me like this should be a windows service rather than a console app. You could communicate between the wpf app and a windows service using a message queue. MSMQ would probably be sufficient. The service would subscribe to the queue and start doing stuff when it gets a go message. Stop when it gets a stop message. Messages are objects so go could include any parameters you like. Commented Dec 6, 2019 at 12:35

2 Answers 2

0

I can suggest you the abstract design of the class . When you click on the start button , in the command handler from the view model or code behind you can fire the task and out your long running code inside the class .

For example:

Your command handler. Where LongRunningLogic is the class where your long running code should go .

  LongRunningLogic _longRunningLogic = new LongRunningLogic();
         Task LongRunningTask = Task.Factory.StartNew(() =>
         {
             _longRunningLogic.LongRunningTask(cts);
         });

Actual method which performs long running operation :

  class LongRunningLogic
{
    public void LongRunningTask(CancellationTokenSource cts)
    {
        while (!cts.IsCancellationRequested )
        {
            //Long running code 
        }
    }
}

To stop this operation on "Stop" click, you can do through CancellationTokenSource. Define a CancellationTokenSource at the class level of the view model and when user clicks on Stop, raise the CancellationTokenSource.

Ex:

CancellationTokenSource cts = new CancellationTokenSource();

On your stop command handler , raise cancellationtokensource like this .

 cts.Cancel();
Sign up to request clarification or add additional context in comments.

3 Comments

I have edit and added console application normal code now I want to pass parameter to it and want to run it. So can I run it from WPF application ?
What you want to run from WPF app ?
from WPF application I want to run external console application. I have place code of WPF for run console application. I want to pass parameter in console application also. So how can I pass parameter to there also ?
0

You may capitalize on the following idea:

public class Runner
{
    private readonly MyClass _object;
    private int _flag;

    public Runner(Object param)
    {
        _object = new MyClass(param);
    }

    public void Start()
    {
        if (Interlocked.CompareExchange(ref _flag, 1, 0) == 0)
        {
            Task.Factory.StartNew(job);
        }
    }

    private void job()
    {
        while (Interlocked.CompareExchange(ref _flag, 1, 1) == 1)
        {
            // do the job on _object
        }
    }

    public void Stop()
    {
        Interlocked.Exchange(ref _flag, 0);
    }
}

You then create a Runner instance in GUI, and call Start in your START button handler, and Stop - inside STOP handler. MyClass and param should be modified accordinally as well.

Your while loop is inside the job function, put your logic there. Then you hit STOP, it would stop on the next loop iteration, so it may take some time. Later you may implement some cancellation logic to react faster.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.