public static void CancelAsync(AsyncTranslateState translateState) { if (translateState == null) { throw new ArgumentNullException("translateState"); } AsyncOperation asyncOp = translateState.AsyncOperation; translateState.RemoveTimers(); TranslateCompletedEventArgs e = new TranslateCompletedEventArgs( translateState, null, true, translateState); // The asyncOp object is responsible for marshaling // the call. try { //some time operation already completed asyncOp.PostOperationCompleted(TranslateCompleted, e); } catch (InvalidOperationException) { } translateState.IsTerminated = true; }
public ReportProgressState( Result translateResult, AsyncTranslateState translateState) { this.translateResult = translateResult; this.translateState = translateState; }
public void AddAdvertisement(AsyncTranslateState translateState) { bool failed = true; foreach (Result r in translateState.Results) { if (!r.ResultNotFound && r.Error == null) { failed = false; break; } } if (failed) { return; } string query = BuildQuery(translateState.Phrase, translateState.TranslatorsSettings, translateState.Results); if (query == currentQuery || string.IsNullOrEmpty(query)) { return; } currentQuery = query; SetExplorerSound(false); advertLoaded = false; wAdvertBrowser.Navigate(new Uri(Constants.StatsPageUrl + "?" + currentQuery)); }
public void SetEndData(AsyncTranslateState state) { bool nothingFound = true; foreach (Result r in state.Results) { if (!r.ResultNotFound) { nothingFound = false; break; } } string htmlString; if (nothingFound && TranslateOptions.Instance.ResultWindowOptions.HideWithoutResult) { htmlString = "<span><br/>" + LangPack.TranslateString("Nothing found") + "<br/></span>"; Wait(); HtmlHelper.AddTranslationCell(wBrowser, isClean, htmlString); } isClean = false; //force set of proper size - some time don't show bottom lines Size sz = wBrowser.Size; sz.Height++; wBrowser.Size = sz; RealRecalcSizes(); sz.Height--; wBrowser.Size = sz; RealRecalcSizes(); }
internal TranslateCompletedEventArgs( AsyncTranslateState translateState, Exception e, bool canceled, object state) : base(e, canceled, state) { this.translateState = translateState; }
public static AsyncTranslateState TranslateAsync(LanguagePair languagePair, string phrase, ReadOnlyServiceSettingCollection translatorsSettings, EventHandler <TranslateProgressChangedEventArgs> progressChangedHandler, EventHandler <TranslateCompletedEventArgs> translateCompletedHandler) { AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(DateTime.Now.Ticks); AsyncTranslateState state = new AsyncTranslateState(languagePair, translatorsSettings, phrase, asyncOp, progressChangedHandler, translateCompletedHandler); state.Run(); return(state); }
internal TranslateProgressChangedEventArgs( Result translateResult, AsyncTranslateState translateState, int progressPercentage, Object userState ) : base(progressPercentage, userState) { this.translateState = translateState; this.translateResult = translateResult; }
internal static void TranslateWorker( ServiceItemSetting translatorSetting, AsyncTranslateState translateState) { if (translateState.IsTerminated) { return; } Result tr = translatorSetting.ServiceItem.Translate(translateState.Phrase, translatorSetting.LanguagePair, translatorSetting.Subject, translatorSetting.NetworkSetting, translateState); ReportProgressState repState = new ReportProgressState(tr, translateState); try { //lock errors when translateState.AsyncOperation.Post(ReportProgress, repState); } catch (InvalidOperationException) { } }
public void SetEndData(AsyncTranslateState state) { bool nothingFound = true; foreach(Result r in state.Results) { if(!r.ResultNotFound) { nothingFound = false; break; } } string htmlString; if(nothingFound && TranslateOptions.Instance.ResultWindowOptions.HideWithoutResult) { htmlString = "<span><br/>" + LangPack.TranslateString("Nothing found")+ "<br/></span>"; Wait(); HtmlHelper.AddTranslationCell(wBrowser, isClean, htmlString); } isClean = false; //force set of proper size - some time don't show bottom lines Size sz = wBrowser.Size; sz.Height++; wBrowser.Size = sz; RealRecalcSizes(); sz.Height--; wBrowser.Size = sz; RealRecalcSizes(); }
internal static void TranslateWorker( ServiceItemSetting translatorSetting, AsyncTranslateState translateState) { if(translateState.IsTerminated) return; Result tr = translatorSetting.ServiceItem.Translate(translateState.Phrase, translatorSetting.LanguagePair, translatorSetting.Subject, translatorSetting.NetworkSetting, translateState); ReportProgressState repState = new ReportProgressState(tr,translateState); try { //lock errors when translateState.AsyncOperation.Post(ReportProgress, repState); } catch(InvalidOperationException) { } }
public static AsyncTranslateState TranslateAsync(LanguagePair languagePair, string phrase, ReadOnlyServiceSettingCollection translatorsSettings, EventHandler<TranslateProgressChangedEventArgs> progressChangedHandler, EventHandler<TranslateCompletedEventArgs> translateCompletedHandler) { AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(DateTime.Now.Ticks); AsyncTranslateState state = new AsyncTranslateState(languagePair, translatorsSettings, phrase, asyncOp, progressChangedHandler, translateCompletedHandler); state.Run(); return state; }
public static void CancelAsync(AsyncTranslateState translateState) { if(translateState == null) throw new ArgumentNullException("translateState"); AsyncOperation asyncOp = translateState.AsyncOperation; translateState.RemoveTimers(); TranslateCompletedEventArgs e = new TranslateCompletedEventArgs( translateState, null, true, translateState); // The asyncOp object is responsible for marshaling // the call. try { //some time operation already completed asyncOp.PostOperationCompleted(TranslateCompleted, e); } catch (InvalidOperationException) { } translateState.IsTerminated = true; }
internal void TranslateCompletedEventHandler(object sender, TranslateCompletedEventArgs e) { if(TranslateOptions.Instance.ResultWindowOptions.ShowQueryStatistics) resBrowser.SetStatistics(DateTime.Now.Ticks - startTranslateTicks); resBrowser.SetEndData(e.TranslateState); ResourceManager resources = new ResourceManager("Translate.Common.Icons", Assembly.GetExecutingAssembly()); miAnimatedIcon.Image = (((System.Drawing.Icon)(resources.GetObject("StaticIcon")))).ToBitmap(); ignoreProfileReposition = true; tsbTranslate.Image = (((System.Drawing.Icon)(resources.GetObject("StaticIcon")))).ToBitmap(); ignoreProfileReposition = false; //don't generate any statistics calls to avoid overloading of googlepages //if(!e.Cancelled && e.Error == null && e.TranslateState.Results.Count > 0) // resBrowser.AddAdvertisement(e.TranslateState); if(activeTranslateState == e.TranslateState) { activeTranslateState = null; } pbMain.Visible = false; StatManager.IncrementCalls(); }
void ATranslateExecute(object sender, EventArgs e) { StopCurrentTranslation(); //force refresh of services TimerRecheckServicesTick(null, null); tbFrom.Text = tbFrom.Text.Trim(); if(Guesser.Enabled && TranslateOptions.Instance.GuessingOptions.SwitchDirectionBasedOnLanguage && detectedText != tbFrom.Text) { NetworkSetting ns = TranslateOptions.Instance.GetNetworkSetting(null); Guesser.Guess(tbFrom.Text, ns, OnGuessCompleted); } ReadOnlyServiceSettingCollection settings = languageSelector.GetServiceSettings();//currentProfile.GetServiceSettings(tbFrom.Text, languageSelector.Selection); if(settings.Count > 50) { if(MessageBox.Show(FindForm(), string.Format( TranslateString("The translation will produce {0} calls to different services, that can overload servers.\nDo you want to interrupt the process of translation ?"), settings.Count) , Constants.AppName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { return; } } if(TranslateOptions.Instance.ResultWindowOptions.ShowQueryStatistics) startTranslateTicks = DateTime.Now.Ticks; ResourceManager resources = new ResourceManager("Translate.Common.Icons", Assembly.GetExecutingAssembly()); miAnimatedIcon.Image = ((System.Drawing.Image)(resources.GetObject("AnimatedIcon"))); ignoreProfileReposition = true; tsbTranslate.Image = ((System.Drawing.Image)(resources.GetObject("AnimatedIcon"))); ignoreProfileReposition = false; if(timerRecheckServices.Enabled) { //pending update of services list - force recheck timerRecheckServices.Stop(); TimerRecheckServicesTick(sender, e); } resBrowser.Stop(); resBrowser.Clear(); resBrowser.Wait(); if(settings.Count > 0) { activeTranslateState = TranslateManager.TranslateAsync(languageSelector.Selection, tbFrom.Text, settings, TranslateProgressChanged, TranslateCompletedEventHandler); pbMain.Value = 7; pbMain.Visible = true; languageSelector.AddSelectionToHistory(); if(!TranslateOptions.Instance.GuessingOptions.SwitchDirectionBasedOnLanguage || tbFrom.Text.Length >= TranslateOptions.Instance.GuessingOptions.MinimalTextLengthForSwitchByLanguage ) TranslateOptions.Instance.ProfilesHistory.AddProfile(currentProfile.Name, languageSelector.Selection.From, detectedLanguage); } else { MessageBox.Show(this, TranslateString("Size or format of query don't supported by available translation services"), Constants.AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void AddAdvertisement(AsyncTranslateState translateState) { bool failed = true; foreach(Result r in translateState.Results) { if(!r.ResultNotFound && r.Error == null) { failed = false; break; } } if(failed) return; string query = BuildQuery(translateState.Phrase, translateState.TranslatorsSettings, translateState.Results); if(query == currentQuery || string.IsNullOrEmpty(query)) return; currentQuery = query; SetExplorerSound(false); advertLoaded = false; wAdvertBrowser.Navigate(new Uri(Constants.StatsPageUrl + "?" + currentQuery)); }