2015-05-21 31 views
1

我慢慢開始進入使用LightSwitch的一段,我們有小程序的項目,但我遇到了,我已經通過幾個職位和日誌看問題並且目前尚未找到解決方案。任何幫助在這裏將不勝感激。LightSwitch的C#MessageBoxResult錯誤(UnautherizedAccessException:無效的跨線程訪問)

作爲一個說明;我正在使用Visual Studio 2013 Ultimate。

我遇到的錯誤是UnauthorizedAccessException was unhandled by user code

我在與正下方粘貼我的問題的代碼段,該段也正在上一個按鈕,用戶點擊調用。這將用於捕獲用戶選擇確定或取消,並根據用戶的選擇執行單獨的操作。

public void Restart_Prompt() 
{ 
    MessageBoxResult result = MessageBox.Show("Yippy", "Hello", MessageBoxButton.OKCancel); 

    if (result == MessageBoxResult.OK) 
    { 
     MessageBox.Show("Selected option was Ok"); 
    } 
    else 
    { 
     MessageBox.Show("Selected option was Cancel..."); 
    } 
} 

再次,任何指針或在這個問題上的援助將不勝感激。

以下是錯誤的詳細文本,如果有人有興趣:

{System.UnauthorizedAccessException: Invalid cross-thread access. 
    at MS.Internal.XcpImports.CheckThread() 
    at MS.Internal.XcpImports.MessageBox_ShowCore(Window window, String messageBoxText, String caption, UInt32 type) 
    at System.Windows.MessageBox.ShowCore(Window window, String messageBoxText, String caption, MessageBoxButton button) 
    at System.Windows.MessageBox.Show(String messageBoxText, String caption, MessageBoxButton button) 
    at LightSwitchApplication.INVENTORiesListDetail.Restart_Prompt() 
    at LightSwitchApplication.INVENTORiesListDetail.Restart_ASI_Execute() 
    at LightSwitchApplication.INVENTORiesListDetail.DetailsClass.MethodSetProperties._Restart_ASI_InvokeMethod(DetailsClass d, ReadOnlyCollection`1 args) 
    at Microsoft.LightSwitch.Details.Framework.Internal.BusinessMethodImplementation`2.<TryInvokeMethod>b__5() 
    at Microsoft.LightSwitch.Utilities.Internal.UserCodeHelper.CallUserCode(Type sourceType, String methodName, String instance, String operation, ILoggingContext context, Action action, String additionalText, Func`1 getCompletedMessage, Boolean tryHandleException, Boolean swallowException, Exception& exception)} 

回答

1

在.NET中 - WPF和WinForms - 用戶界面是線程仿射。換句話說,所有與UI的交互必須發生在UI線程上。在WPF中,這是通過Dispatcher類型實現的,而在WinForms中則是通過每個控件的Invoke方法實現。您的方法正在從後臺線程調用,因此您需要將調用編組到UI線程以防止出現錯誤。在燈開關我認爲是這樣的:

Dispatchers.Main.BeginInvoke(()=> 
{ 
    // message box method call here 
}); 
+0

添加'使用Microsoft.LightSwitch.Threading'使用語句和您的解決方案爲我工作......感謝指針那裏。 – DAS03590

相關問題