【问题标题】:how to Run Function parallelly in windows Application如何在 Windows 应用程序中并行运行函数
【发布时间】:2014-09-05 10:04:06
【问题描述】:

在我的 Windows 应用程序中,我有一个必须每 6 秒运行一次的功能。 所以,我正在使用System.windows.forms.Timer 工具栏 -> 设置它的间隔 6000ms 并在该计时器滴答事件上调用该函数

     private void tmrConnectionStatus_Tick(object sender, EventArgs e)
         {
             CheckClientConnectinStatus();
         }

它的工作完美, 但是当我通过下面的函数调用其他 Windows 应用程序时.. 直到或除非我从该应用程序返回到主应用程序,它才会工作

                 clsLogs.LogEvent(3, "==============Call to PNS ==============");
                        ProcessStartInfo startInfo = new ProcessStartInfo();
                        startInfo.FileName = strVendorfile; //Vendors exe
                        startInfo.Arguments = "";           //CSV file path

                        clsLogs.LogEvent(1, "SmartTracker to PRINT-N-SPECT:" + System.DateTime.Now);
                        objProcess = Process.Start(startInfo);       
 Lable:
                        Process[] appProc;
                        string strModName, strProcName;
                        strModName = Process.GetCurrentProcess().MainModule.ModuleName;
                        strProcName = "PnS";
                        appProc = Process.GetProcessesByName(strProcName);

                        if (appProc.Length > 0)
                        {
                            Thread.Sleep(5000);
                            goto Lable;
                        }

我想知道是否有任何方法可以使用计时器或其他方法在后台连续工作,即使我专注于其他应用程序。

【问题讨论】:

标签: c# timer backgroundworker background-process


【解决方案1】:

你可以使用backgroundworker。它将在后台工作。

【讨论】:

    【解决方案2】:

    这篇文章提供了一个很好的比较,应该包含你需要的信息:Comparing the Timer Classes in the .NET Framework Class Library:

                                        Windows.Forms  System.Timers         System.Threading 
    Timer event runs on what thread?    UI thread      UI or worker thread   Worker thread 
    Instances are thread safe?          No             Yes                   No 
    Familiar/intuitive object model?    Yes            Yes                   No 
    Requires Windows Forms?             Yes            No                    No 
    Metronome-quality beat?             No             Yes*                  Yes* 
    Timer event supports state object?  No             No                    Yes 
    Initial timer event schedulable?    No             No                    Yes 
    Class supports inheritance?         Yes            Yes                   No
    
    * Depending on the availability of system resources (for example, worker threads) 
    

    注意: 还有一个System.Windows.Forms.Timer 类。该类基于操作系统计时器支持,如果您没有在线程上发送消息,您的计时器将不会发生。这使得System.Threading.Timer 在许多情况下更加有用。

    【讨论】:

      猜你喜欢
      • 2014-10-28
      • 2017-10-01
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多