简体   繁体   English

如何从 WPF 开始按钮运行任务计划程序并在 C# 中发送参数?

[英]How to run task schedular program from WPF start button with sending parameters in c#?

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.我正在开发 wpf 应用程序,其中有很多 UI 屏幕可用,并且每个 UI 都包含使用多线程、任务等待、调度程序和后台工作程序进行的大量处理。 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.现在,当我单击 START 按钮时,整个应用程序的进程(长时间运行的进程)需要在 while 循环中运行。 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.现在我想实现,当我从我的 WPF 应用程序单击开始按钮时,另一个应用程序(仅包含 cs 文件,不会有任何类型的 UI)需要在 while 循环中运行。 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.为此,我有一个 WPF 应用程序,第二个是控制台应用程序。 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.现在我需要从我的 WPF 应用程序传递字符串参数,并希望从我的 WPF 应用程序运行这个控制台应用程序。 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.当我单击“开始”按钮时,从 WPF 应用程序开始,然后我想运行带有传递参数的控制台应用程序。 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.那么如何将参数传递给控制台应用程序并运行控制台应用程序。

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 _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.要在“停止”单击时停止此操作,您可以通过 CancellationTokenSource 来完成。 Define a CancellationTokenSource at the class level of the view model and when user clicks on Stop, raise the CancellationTokenSource.在视图模型的类级别定义一个 CancellationTokenSource,当用户单击停止时,引发 CancellationTokenSource。

Ex:前任:

CancellationTokenSource cts = new CancellationTokenSource();

On your stop command handler , raise cancellationtokensource like this .在您的停止命令处理程序上,像这样引发取消令牌源。

 cts.Cancel();

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.然后您在 GUI 中创建一个Runner实例,并在您的 START 按钮处理程序中调用Start ,并在 STOP 处理程序中调用Stop - inside STOP 处理程序。 MyClass and param should be modified accordinally as well. MyClassparam应该相应地修改。

Your while loop is inside the job function, put your logic there.您的 while 循环位于job功能内,将您的逻辑放在那里。 Then you hit STOP, it would stop on the next loop iteration, so it may take some time.然后你点击 STOP,它会在下一次循环迭代中停止,所以可能需要一些时间。 Later you may implement some cancellation logic to react faster.稍后您可能会实现一些取消逻辑以更快地做出反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM