मुझे अपने आवेदन में कोई समस्या है: किसी बिंदु पर, सिंक्रनाइज़ेशन कॉन्टेक्स्ट.क्यूरेंट मुख्य धागे के लिए शून्य हो जाता है। मैं एक अलग परियोजना में एक ही समस्या को पुन: उत्पन्न करने में असमर्थ हूं। मेरा असली परियोजना जटिल है; यह विंडोज फॉर्म और डब्ल्यूपीएफ मिश्रण करता है और डब्ल्यूसीएफ वेब सर्विसेज को कॉल करता है। जहां तक मुझे पता है, वे सभी सिस्टम हैं जो सिंक्रनाइज़ेशन कॉन्टेक्स्ट के साथ बातचीत कर सकते हैं।सिंक्रनाइज़ेशन कॉन्टेक्स्ट। मुख्य थ्रेड का वर्तमान विंडोज़ फॉर्म एप्लिकेशन में कैसे शून्य हो सकता है?
यह मेरी अलग परियोजना से कोड है। मेरा असली ऐप ऐसा कुछ करता है जो उसके जैसा दिखता है। हालांकि, मेरे असली ऐप में सिंक्रनाइज़ेशन कॉन्टेक्स्ट। कंटेंट मुख्य थ्रेड पर शून्य है जब निरंतरता कार्य निष्पादित किया जाता है।
private void button2_Click(object sender, EventArgs e)
{
if (SynchronizationContext.Current == null)
{
Debug.Fail("SynchronizationContext.Current is null");
}
Task.Factory.StartNew(() =>
{
CallWCFWebServiceThatThrowsAnException();
})
.ContinueWith((t) =>
{
//update the UI
UpdateGUI(t.Exception);
if (SynchronizationContext.Current == null)
{
Debug.Fail("SynchronizationContext.Current is null");
}
}, CancellationToken.None,
TaskContinuationOptions.OnlyOnFaulted,
TaskScheduler.FromCurrentSynchronizationContext());
}
क्या मुख्य थ्रेड के SynchronizationContext.Current कारण अशक्त बनने के लिए कर सकता है?
संपादित करें:
@Hans स्टैक ट्रेस के लिए कहा। संदेश यह है:
अगर यह पसंदीदा तरीका है, लेकिन यहाँ मैं कैसे SynchronizationContext का उपयोग हैat MyApp.Framework.UI.Commands.AsyncCommand.HandleTaskError(Task task) in d:\sources\s2\Framework\Sources\UI\Commands\AsyncCommand.cs:line 157 at System.Threading.Tasks.Task.c__DisplayClassb.b__a(Object obj) at System.Threading.Tasks.Task.InnerInvoke() at System.Threading.Tasks.Task.Execute() at System.Threading.Tasks.Task.ExecutionContextCallback(Object obj) at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot) at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution) at System.Threading.Tasks.SynchronizationContextTaskScheduler.PostCallback(Object obj) at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner) at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) at System.Delegate.DynamicInvokeImpl(Object[] args) at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme) at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj) at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme) at System.Windows.Forms.Control.InvokeMarshaledCallbacks() at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at MyApp.Framework.SharedUI.ApplicationBase.InternalStart() in d:\sources\s2\Framework\Sources\UI\SharedUI\ApplicationBase.cs:line 190 at MyApp.Framework.SharedUI.ApplicationBase.Start() in d:\sources\s2\Framework\Sources\UI\SharedUI\ApplicationBase.cs:line 118 at MyApp.App1.WinUI.HDA.Main() in d:\sources\s2\App1\Sources\WinUI\HDA.cs:line 63
UpdateGUI पर ब्रेकपॉइंट सेट करें और स्टैक ट्रेस पोस्ट करें। –
@ हंस: UpdateGUI() मेरे नमूना प्रोजेक्ट में है। मेरे असली प्रोजेक्ट में, विधि को हैंडलटास्क एरर() कहा जाता है। मैंने अपने प्रश्न में स्टैक ट्रेस पोस्ट किया है। – Sylvain
पूरी तरह से सामान्य लग रहा है। मुझे कोई स्पष्टीकरण नहीं है कि TaskScheduler.FromCurrentSynchronizationContext() काम नहीं करता है। यही वह है जो नियंत्रण के लिए अनुसूचित जाति प्रदान करता है। इन्वोक मार्शल कॉलबैक() कॉल। यह पहले से ही एक शून्य के लिए जाँच करता है। मान लीजिए कि आप .NET 4.0 –