天道酬勤,学无止境

invalidoperationexception

InvalidOperationException vs. ArgumentException [closed]

问题 很难说这里问的是什么。 这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。 如需帮助澄清此问题以便重新打开它,请访问帮助中心。 8年前关闭。 我知道摘要和描述。 但是如果 ARGUMENT 处于无效状态怎么办? 我认为 ArgumentException 更合适,因为 InvalidOperationException 文档说,调用该方法的对象本身必须处于无效状态。 这有意义吗? 回答1 文档说得很清楚: InvalidOperationException 用于由于无效参数以外的原因导致调用方法失败的情况。 ... 如果方法调用失败是由于参数无效,则应改为抛出 ArgumentException 或其派生类之一 ArgumentNullException 或 ArgumentOutOfRangeException。 您的方法期望参数处于某种状态,这可能包括任何内容,包括处于由参数类型本身定义的“有效状态”。 我认为主要区别在于问题的根源: 是调用方法的参数还是对象? 回答2 您可能因缺乏示例而错过了。 ArgumentException :示例将是一个函数由于传递给该函数的参数无效而导致异常。 使用在给定情况下理想的任何防御机制来正确处理这种异常。 InvalidOperationException :示例将在枚举器生效时修改集合

2022-05-05 16:58:08    分类:技术分享    c#   invalidoperationexception   argumentexception

InvalidOperationException when multithreading in WPF

问题 所以我正在开发我的第一个多线程 WPF 应用程序。 请记住,我对如何实现多线程几乎一无所知——我已经在一些应用程序中完成了它,并且总是使用现有代码。 这是我在 WPF 中的第一次尝试,这显然与 Windows 窗体完全不同...... 所以基本上我正在研究这个例子,奇怪的是没有提到你必须实例化一个新线程并启动它——我猜作者觉得即使对像我这样的新手来说也是不言而喻的。 在任何情况下,它都可以正常工作,直到我想更新我的 UI 控件的属性,此时我遇到了InvalidOperationException ,它说调用线程无法访问控件,因为控件属于不同的线程 所以基本上这不是一种线程安全的工作方式,但我不知道应该如何解决这个问题...... 这是我的代码: string fn = ""; private void btnBrowse_Click(object sender, RoutedEventArgs e) { if (lblActivity.Visibility != System.Windows.Visibility.Hidden) { lblActivity.Visibility = System.Windows.Visibility.Hidden; } if (lstResult.Items.Count != 0) { lstResult.Items.Clear(); }

2022-05-05 01:05:33    分类:技术分享    wpf   multithreading   invalidoperationexception

InvalidOperationException when multithreading in WPF

So I'm working on my first multithreaded WPF app. Please bear in mind I have little to no understanding of how to implement multithreading - I've done it in a few apps and always work off the existing code. This is my first attempt at it in WPF which is apparently quite different to Windows Forms... So basically I'm working on this example which oddly enough doesn't mention that you have to instantiate a new thread and start it - I guess the author felt that was self evident even to newbies like myself. In any case, it works okay until I get to the point where I want to update the properties

2022-04-28 11:49:39    分类:问答    wpf   multithreading   invalidoperationexception

"InvalidOperationException: Object is currently in use elsewhere" during innocuous onpaint?

For some reason we are getting "InvalidOperationException: Object is currently in use elsewhere." during our custom OnPaint, below (that's actually almost a line for line copy of the code... there's that little there). We have logging in the exception handler below to detect if we're somehow calling OnPaint from a non-UI thread... and that isn't getting tripped, but we are getting that error logged (see stack trace below). On machines where we're getting these errors, we're also seeing the dreaded Red X of doom from other controls (which presumably don't have a try/catch around their OnPaints)

2022-04-28 05:20:25    分类:问答    c#   paint   invalidoperationexception

InvalidOperationException vs. ArgumentException [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 8 years ago. I know the summaries and descriptions. But what if the ARGUMENT is in an INVALID STATE? I think the ArgumentException is more appropriate because the InvalidOperationException documentation says that the object on what the method is called itself has to be in an invalid state. Does this make sense?

2022-04-27 05:10:26    分类:问答    c#   invalidoperationexception   argumentexception

instance of type cannot be tracked because of another instance with same ID error

I have a .net core 2.1 mvc application with EF Core where I use automapper to match viewmodels with domainmodels. In my edit method I get the error : InvalidOperationException: The instance of entity type 'Ticket' cannot be tracked because another instance with the same key value for {'ID'} is already being tracked. The several other topics here did not solve my problem. My Edit method: [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Edit(int id, TicketViewModel ticketViewModel) { Ticket ticketToUpdate = await _unitOfWork.Ticket.Get(id); // I implement unit of work //

2022-04-17 06:54:25    分类:问答    c#   entity-framework   asp.net-core-mvc   automapper   invalidoperationexception

nullable object must have a value

There is paradox in the exception description: Nullable object must have a value (?!) This is the problem: I have a DateTimeExtended class, that has { DateTime? MyDataTime; int? otherdata; } and a constructor DateTimeExtended(DateTimeExtended myNewDT) { this.MyDateTime = myNewDT.MyDateTime.Value; this.otherdata = myNewDT.otherdata; } running this code DateTimeExtended res = new DateTimeExtended(oldDTE); throws an InvalidOperationException with the message: Nullable object must have a value. myNewDT.MyDateTime.Value - is valid and contain a regular DateTime object. What is the meaning of this

2022-03-26 05:08:04    分类:问答    c#   nullable   invalidoperationexception

Why won't this Audio play in Windows Phone 7?

I am trying to play audio in my Silverlight for Windows Phone 7 app. I have an MP3 audio file with its build action set to resource. To play the sound, I use: SoundEffectInstance sfi = null; ... Stream source = Application.GetResourceStream(new Uri("/Bird Calls;component/Crow.mp3", UriKind.Relative)).Stream; Microsoft.Xna.Framework.Audio.SoundEffect effect = SoundEffect.FromStream(source); sfi = effect.CreateInstance(); sfi.Play(); This code throws a InvalidOperationException at the SoundEffect.FromStream method.

2022-03-24 05:23:12    分类:问答    c#   windows-phone-7   audio   invalidoperationexception   soundeffect