Exemplo n.º 1
0
        private long MeasureProgressUpdateCost(bool doMakeProgressCalls, int iterationsToDo)
        {
            _dialog = new ProgressDialog();
            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork           += OnDoSomeWork;
            _dialog.BackgroundWorker = worker;
            WorkArguments args = new WorkArguments();

            args.doMakeProgressCalls        = doMakeProgressCalls;
            args.secondsToUseUp             = 0;
            args.iterationsToDo             = iterationsToDo;
            _dialog.ProgressState.Arguments = args;
            Stopwatch w = new Stopwatch();

            w.Start();
            _dialog.ShowDialog();
            _dialog.Close();
            _dialog.Dispose();
            _dialog = null;
            w.Stop();
            worker.Dispose();
            Debug.WriteLine("Took " + w.Elapsed);
            return(w.ElapsedMilliseconds);
        }
Exemplo n.º 2
0
        private void OnDoSomeWork(object sender, DoWorkEventArgs e)
        {
            ProgressState state = (ProgressState)e.Argument;
            WorkArguments args  = state.Arguments as WorkArguments;

            if (args == null)
            {
                args = new WorkArguments();               //use defaults
            }
            state.TotalNumberOfSteps = 10000;             //we actually have no idea, but let it be large
            try
            {
                _argumentReceivedFromProgressState = args.dummy;
                if (args.secondsToUseUp > 0)
                {
                    DateTime end = DateTime.Now.AddSeconds(args.secondsToUseUp);
                    while (DateTime.Now < end)
                    {
                        state.WriteToLog(_countForWork.ToString());
                        Thread.Sleep(10);
                        if (args.doMakeProgressCalls)
                        {
                            state.NumberOfStepsCompleted++;
                        }
                    }
                }
                else                  //seeing how long this takes
                {
                    state.TotalNumberOfSteps = args.iterationsToDo;
                    double a = 0;
                    for (int i = 0; i < state.TotalNumberOfSteps; i++)
                    {
                        //adding this makes freeze not happen Thread.Sleep(1);
                        a += Math.Sqrt(987987 + i);
                        if (args.doMakeProgressCalls)
                        {
                            state.NumberOfStepsCompleted++;
                        }
                    }
                }
                if (args.doClaimExtraSteps)
                {
                    state.NumberOfStepsCompleted = state.TotalNumberOfSteps + 5;
                }
                e.Result = "all done";
            }
            catch (Exception err)
            {
                //currently, error reporter can choke because this is
                //being called from a non sta thread.
                //so let's leave it to the progress dialog to report the error
                //                Reporting.ErrorReporter.ReportException(e,null, false);
                state.ExceptionThatWasEncountered = err;
                state.WriteToLog(err.Message);
                state.State = ProgressState.StateValue.StoppedWithError;
            }
        }
Exemplo n.º 3
0
        public void DontDieIfWorkerTriesToReportExcessSteps()
        {
            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork           += OnDoSomeWork;
            _dialog.BackgroundWorker = worker;
            WorkArguments args = new WorkArguments();

            args.doMakeProgressCalls        = true;
            args.doClaimExtraSteps          = true;
            _dialog.ProgressState.Arguments = args;
            _dialog.ShowDialog();
        }
Exemplo n.º 4
0
        public void SendArgumentsToWorker()
        {
            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork           += OnDoSomeWork;
            _dialog.BackgroundWorker = worker;
            WorkArguments args = new WorkArguments();

            args.dummy = "testing";

            _dialog.ProgressState.Arguments = args;
            Assert.AreNotEqual("testing", _argumentReceivedFromProgressState);
            _dialog.ShowDialog();
            Assert.AreEqual("testing", _argumentReceivedFromProgressState);
            Assert.AreEqual(DialogResult.OK, _dialog.DialogResult);
        }
        public async Task <bool> ExecuteWorkerAsync(WorkArguments args, IJobCancellationToken cancellationToken)
        {
            try
            {
                _logger.LogInformation("Trying to create worker instance using {@data}", args);
                var wType = Type.GetType(args.GetFullQualifiedWorkerClassName());
                //var wObj = (IWorker)ActivatorUtilities.CreateInstance(_provider, wType);
                var wObj = (IWorker)_provider.GetService(wType);
                _logger.LogInformation("Now executing worker/userJobId {i}", args.UserJobId);
                var ret = await wObj.DoWorkAsync(args, cancellationToken);

                _logger.LogInformation("Finished executing worker/userJobId {id}. Return value is {value}", args.UserJobId, ret);
                return(ret);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Unhandled exception for worker {@data}", args);
                throw;
            }
        }
Exemplo n.º 6
0
        public override async Task <bool> DoWorkAsync(WorkArguments args, IJobCancellationToken token)
        {
            var ret = false;

            try
            {
                for (var i = 0; i < 5; i++)
                {
                    token.ThrowIfCancellationRequested();
                    _logger.LogInformation("#{i}.This is from download all product worker. JobType {j}, UserJobId {k}", i, args.JobType.ToString(), args.UserJobId);
                    await Task.Delay(5000);

                    ret = true;
                }
            }
            catch (Exception)
            {
                _logger.LogWarning("Job cancelled JobType {i}, UserJobId {j}", args.JobType.ToString(), args.UserJobId);
            }

            _logger.LogInformation("Job ended JobType {i}, UserJobId {j}", args.JobType.ToString(), args.UserJobId);
            return(ret);
        }
Exemplo n.º 7
0
 public abstract Task <bool> DoWorkAsync(WorkArguments args, IJobCancellationToken token);
Exemplo n.º 8
0
		public void SendArgumentsToWorker()
		{
			BackgroundWorker worker = new BackgroundWorker();
			worker.DoWork += OnDoSomeWork;
			_dialog.BackgroundWorker = worker;
			WorkArguments args  = new WorkArguments();
			args.dummy = "testing";

			_dialog.ProgressState.Arguments = args;
			Assert.AreNotEqual("testing", _argumentReceivedFromProgressState);
			_dialog.ShowDialog();
			Assert.AreEqual("testing", _argumentReceivedFromProgressState);
			Assert.AreEqual(DialogResult.OK, _dialog.DialogResult);
		}
Exemplo n.º 9
0
		private void OnDoSomeWork(object sender, DoWorkEventArgs e)
		{
			ProgressState state = (ProgressState) e.Argument;
			WorkArguments args = state.Arguments as WorkArguments;
			if (args == null)
			{
				args = new WorkArguments(); //use defaults
			}
			state.TotalNumberOfSteps = 10000; //we actually have no idea, but let it be large
			try
			{
				_argumentReceivedFromProgressState = args.dummy;
				if (args.secondsToUseUp > 0)
				{
					DateTime end = DateTime.Now.AddSeconds(args.secondsToUseUp);
					while (DateTime.Now < end)
					{
						state.WriteToLog(_countForWork.ToString());
						Thread.Sleep(10);
						if (args.doMakeProgressCalls)
						{
							state.NumberOfStepsCompleted++;
						}
					}
				}
				else  //seeing how long this takes
				{
					state.TotalNumberOfSteps = args.iterationsToDo;
					double a = 0;
					for (int i = 0; i < state.TotalNumberOfSteps; i++)
					{
						//adding this makes freeze not happen Thread.Sleep(1);
						a += Math.Sqrt(987987+i);
						if (args.doMakeProgressCalls)
						{
							state.NumberOfStepsCompleted++;
						}
					}
				}
				if(args.doClaimExtraSteps)
				{
					state.NumberOfStepsCompleted = state.TotalNumberOfSteps + 5;
				}
				e.Result = "all done";
			}
			catch (Exception err)
			{
				//currently, error reporter can choke because this is
				//being called from a non sta thread.
				//so let's leave it to the progress dialog to report the error
				//                Reporting.ErrorReporter.ReportException(e,null, false);
				state.ExceptionThatWasEncountered = err;
				state.WriteToLog(err.Message);
				state.State = ProgressState.StateValue.StoppedWithError;
			}
		}
Exemplo n.º 10
0
		private long MeasureProgressUpdateCost(bool doMakeProgressCalls, int iterationsToDo)
		{
			_dialog = new ProgressDialog();
			BackgroundWorker worker = new BackgroundWorker();
			worker.DoWork += OnDoSomeWork;
			_dialog.BackgroundWorker = worker;
			WorkArguments args = new WorkArguments();
			args.doMakeProgressCalls = doMakeProgressCalls;
			args.secondsToUseUp = 0;
			args.iterationsToDo = iterationsToDo;
			_dialog.ProgressState.Arguments = args;
			Stopwatch w = new Stopwatch();
			w.Start();
			_dialog.ShowDialog();
			_dialog.Close();
			_dialog.Dispose();
			_dialog = null;
			w.Stop();
			worker.Dispose();
			Debug.WriteLine("Took "+ w.Elapsed);
			return w.ElapsedMilliseconds;
		}
Exemplo n.º 11
0
		public void DontDieIfWorkerTriesToReportExcessSteps()
		{
			BackgroundWorker worker = new BackgroundWorker();
			worker.DoWork += OnDoSomeWork;
			_dialog.BackgroundWorker = worker;
			WorkArguments args = new WorkArguments();
			args.doMakeProgressCalls = true;
			args.doClaimExtraSteps = true;
			_dialog.ProgressState.Arguments = args;
			_dialog.ShowDialog();
		}
Exemplo n.º 12
0
 public override async Task <bool> DoWorkAsync(WorkArguments args, IJobCancellationToken token)
 {
     Console.WriteLine("GqlDownLoader finished Working");
     return(await Task.FromResult <bool>(true));
 }