Ejemplo n.º 1
0
 /// <summary>
 /// Called when [task executing].
 /// </summary>
 /// <param name="task">The task.</param>
 internal void OnTaskExecuting(IScheduledTaskWorker task)
 {
     TaskExecuting?.Invoke(this, new GenericEventArgs <IScheduledTaskWorker>
     {
         Argument = task
     });
 }
Ejemplo n.º 2
0
        private void PerformUpdate()
        {
            if (StartupArguments.MCSyncProcessID != -1)
            {
                NonTaskExecuting?.Invoke(this, "Waiting for MCSync to close...");
                while (Process.GetProcesses().Any(p => p.Id == StartupArguments.MCSyncProcessID))
                {
                    Thread.Sleep(50);
                }
            }

            NetworkCredential ftpCredentials = new NetworkCredential("Reporting", "NetLogon");
            FtpWebRequest     webRequest     = (FtpWebRequest)WebRequest.Create($"ftp://www.clussmanproductions.com/support/MCSyncNew/updates/{StartupArguments.VersionToDownload}");

            webRequest.Method      = WebRequestMethods.Ftp.ListDirectory;
            webRequest.Credentials = ftpCredentials;

            string files;

            try
            {
                using (Stream responseStream = webRequest.GetResponse().GetResponseStream())
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        files = reader.ReadToEnd();
                    }
            }
            catch (WebException _)
            {
                InvalidVersion?.Invoke(this, new EventArgs());
                return;
            }

            using (StringReader reader = new StringReader(files))
            {
                string file;

                int counter = 0;
                while ((file = reader.ReadLine()) != null)
                {
                    counter++;
                }

                NumberOfTasks?.Invoke(this, counter);
            }

            string directoryPrefix = "";

            if (!string.IsNullOrEmpty(StartupArguments.FolderToDelete) && Directory.GetCurrentDirectory().Contains(StartupArguments.FolderToDelete))
            {
                directoryPrefix = "..\\";
            }
            using (StringReader reader = new StringReader(files))
            {
                string file;
                while ((file = reader.ReadLine()) != null)
                {
                    TaskExecuting?.Invoke(this, "Downloading " + file);

                    webRequest             = (FtpWebRequest)WebRequest.Create($"ftp://www.clussmanproductions.com/support/MCSyncNew/updates/{StartupArguments.VersionToDownload}/{file}");
                    webRequest.Credentials = ftpCredentials;
                    webRequest.Method      = WebRequestMethods.Ftp.DownloadFile;

                    using (Stream responseStream = webRequest.GetResponse().GetResponseStream())
                        using (Stream fileStream = File.Create(directoryPrefix + file))
                        {
                            responseStream.CopyTo(fileStream);
                        }
                }
            }

            StringBuilder argumentBuilder = new StringBuilder($"-processID {Process.GetCurrentProcess().Id}");

            if (!string.IsNullOrEmpty(StartupArguments.FolderToDelete))
            {
                argumentBuilder.Append($" -folder {StartupArguments.FolderToDelete}");
            }

            NonTaskExecuting?.Invoke(this, "Starting MCSync...");
            ProcessStartInfo startInfo = new ProcessStartInfo(directoryPrefix + "MesaSuite.exe", argumentBuilder.ToString());

            startInfo.WorkingDirectory = Directory.GetCurrentDirectory() + directoryPrefix;
            Process.Start(startInfo);
        }
Ejemplo n.º 3
0
        private async Task Next(CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            if (!_isBusy)
            {
                if (queue.Count > 0)
                {
                    // Optional delay.

                    if (Delay > default(TimeSpan))
                    {
                        await Task.Delay(Delay);
                    }

                    // Peek the current task.

                    var context = queue.Peek();

                    try
                    {
                        _isRunning = true;
                        _isBusy    = true;

                        // Execute the current task.

                        TaskExecuting?.Invoke(this, new TaskEventArgs(context.Tag));

                        await context.Action(context, cts.Token);
                    }
                    catch (Exception exc)
                    {
                        // Handle any exception thrown inside a task.
                        // Invoke the Exception event handlers.

                        TaskException?.Invoke(this, new TaskEventArgs(context.Tag, exc));

                        if (CancelOnException)
                        {
                            // Cancel the queue.

                            ClearInternal();
                        }
                    }

                    TaskExecuted?.Invoke(this, new TaskEventArgs(context.Tag));

                    // Dequeue the currently finished task and request the next.

                    if (queue.Count > 0)
                    {
                        queue.Dequeue();
                    }
                    _isBusy = false;
                    await Next(cts.Token);
                }
            }
            else
            {
                _isRunning = false;
            }
        }
 /// <summary>
 /// 通知工作线程即将执行任务。
 /// </summary>
 /// <param name="e"></param>
 protected virtual void OnTaskExecuting(TaskExecutingEventArgs <TTask> e)
 {
     TaskExecuting?.Invoke(this, e);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 通知工作线程即将执行任务。
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void OnTaskExecuting(object sender, TaskExecutingEventArgs <TTask> e)
 {
     IncrementWorkThread();
     TaskExecuting?.Invoke(sender, e);
 }