private void GetPath() { #region Task实现 _igetsrv = new GetPath { SaveFilePath = SaveTextBox.Text.Trim(), //BasePath = BasePathTextBox.Text.Trim(), // FileXpath = fileXpath.Text.Trim(), //NetPath = AddressTextBox.Text.Trim(), // PropertyName = PropertyName.Text.Trim() }; if (_myTask != null && !_myTask.IsCompleted) { Invoke(new MethodInvoker(() => MessageBox.Show("正在执行操作请耐心等待"))); return; } _myTask = Task.Factory.StartNew(() => { Invoke(new MethodInvoker(() => MessageBox.Show("正在执行操作请耐心等待……"))); _igetsrv.GetService(_fileTypeId); Invoke(new MethodInvoker(() => MessageBox.Show("完成了操作"))); }); #endregion #region Backgroudworker 实现 //if (!backgroundWorker1.IsBusy) //{ #region 线程池实现异步 //线程池实现异步 //ThreadPool.QueueUserWorkItem(item => //{ //Igetsrv = new GetPath( ); //Igetsrv._saveFilePath = this.SaveTextBox.Text.Trim( ); //Igetsrv._basePath = this.BasePathTextBox.Text.Trim( ); //Igetsrv._fileXpath = this.fileXpath.Text.Trim( ); //Igetsrv._netPath = this.AddressTextBox.Text.Trim( ); //Igetsrv._PropertyName = this.PropertyName.Text.Trim( ); //Igetsrv.GetService(_file_type_id); //Invoke(new MethodInvoker(( ) => MessageBox.Show("完成了操作"))); //backgroundWorker1.RunWorkerAsync( ); //} //); //Invoke(new MethodInvoker(( ) => MessageBox.Show("正在执行操作请耐心等待"))); #endregion //} // else // { // MessageBox.Show("正在进行其他操作"); // } #endregion }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { if (backgroundWorker1.CancellationPending) { e.Cancel = true; } else { _igetsrv.GetService(_fileTypeId); } }
/// <summary> /// Causes the container to instantiate the service with the given /// <paramref name="serviceType">service type</paramref>. If the service type cannot be created, then an /// exception will be thrown if the <see cref="IContainer.SuppressErrors" /> property /// is set to false. Otherwise, it will simply return null. /// </summary> /// <param name="serviceName">The name of the service to instantiate.</param> /// <param name="serviceType">The service type to instantiate.</param> /// <param name="additionalArguments">The additional arguments that will be used to instantiate the service type.</param> /// <returns> /// If successful, it will return a service instance that is compatible with the given type; /// otherwise, it will just return a <c>null</c> value. /// </returns> public virtual object GetService(string serviceName, Type serviceType, params object[] additionalArguments) { IFactory factory = null; if (FactoryStorage != null) { factory = FactoryStorage.GetFactory(serviceName, serviceType, additionalArguments); } var serviceRequest = new ServiceRequest(serviceName, serviceType, additionalArguments, factory, this); var instance = _getServiceBehavior.GetService(serviceRequest); if (SuppressErrors == false && instance == null && serviceName == null) { throw new ServiceNotFoundException(serviceType); } if (SuppressErrors == false && instance == null && serviceName != null) { throw new NamedServiceNotFoundException(serviceName, serviceType); } return(instance); }