标签: invalidoperationexception

为什么我会收到InvalidOperationException?

foreach (var shotItem in Invadershots)// it points to me to there and doesnt allow me to loop.."{"Collection was modified; enumeration operation may not execute."}"
{
  shotItem.Move();// it happens when this simple method called (which actually checks some bool..if the shot was out of the winform).
  if (shotItem.removeShot)
  {
        Invadershots.Remove(shotItem);
  }
}
Run Code Online (Sandbox Code Playgroud)

可能是因为我同时更改了列表项吗?
如何防止发生错误?

c# invalidoperationexception winforms

1
推荐指数
1
解决办法
166
查看次数

"InvalidOperationException:Object目前正在其他地方使用"在无害的onpaint期间?

出于某种原因,我们得到"InvalidOperationException:Object目前正在其他地方使用."

在我们的自定义OnPaint中,下面(实际上几乎是一行代码的行副本......那里有那么少).

我们已经登录下面的异常处理程序来检测我们是否以某种方式从非UI线程调用OnPaint ......并且没有被触发,但我们记录了该错误(请参阅下面的堆栈跟踪).

在我们遇到这些错误的机器上,我们也看到了来自其他控件的可怕的Red X ofom(可能在他们的OnPaints周围没有try/catch).

它们可能是相关的,但如果仅从UI线程调用此代码,我无法弄清楚可能导致该错误的原因.

有任何想法吗?

这是堆栈跟踪:

System.InvalidOperationException:Object目前正在其他地方使用.
在 System.Windows.Fornd.DtrolBorderSimple的
System.Drawing.Graphics.DrawRectangle(Pen pen,Int32 x,Int32 y,Int32 width,Int32 height)的System.Drawing.Graphics.CheckErrorStatus(Int32 status)处
(图形图形,矩形范围,Colour彩色,ButtonBorderStyle风格)
在System.Windows.Forms.ControlPaint.DrawBorder(图形图像,矩形范围,Colour彩色,ButtonBorderStyle风格)
在MyUserControl.OnPaint(PaintEventArgs的E)

这是班级:

public class MyUserControl : UserControl
{
    // Override this to set your custom border color
    protected Color mBorderColor = SystemColors.ControlDarkDark;

    public MyeUserControl()
        : base()
    {
        this.BorderStyle = BorderStyle.None;
        this.Padding = new Padding(1);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        try
        {
            ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, mBorderColor, ButtonBorderStyle.Solid);
        }
        catch (Exception ex)
        {
            // check if we're not …
Run Code Online (Sandbox Code Playgroud)

c# paint invalidoperationexception

1
推荐指数
1
解决办法
3554
查看次数

EF4代码第一个流畅的映射不适用于继承属性?

有没有人尝试过映射继承的属性?因为我很高兴听到它正常工作并且我在某处犯了错误,因为我收到以下错误:

"属性'UserName'不是'Advertiser'类型的声明属性.通过使用Ignore方法或NotMappedAttribute数据注释验证该属性是否未从模型中显式排除.请确保它是有效的原始属性."

我的模型看起来像这样:

abstract class Entity { public int Id {get; set; }}
abstract class User : Entity { public string UserName {get; set;} }
sealed class Advertiser : User { }
Run Code Online (Sandbox Code Playgroud)

我的AdvertisementConfiguration类看起来像这样:

class AdvertiserConfiguration : EntityTypeConfiguration<Advertiser>
{
   public AdvertiserConfiguration()
   {
      // the following line indirectly causes an InvalidOperationException:
      Property( x => x.UserName ).HasMaxLength(50);
   }
}
Run Code Online (Sandbox Code Playgroud)

如果我更改广告商类以使其不从User继承(并将UserName属性拉下来),那么一切正常.

entity-framework invalidoperationexception code-first entity-framework-4 ef-code-first

1
推荐指数
1
解决办法
2367
查看次数

使用空结构捕获InvalidOperationException

我试图抓住InvalidOperationException在声明变量时有时会发生的事情.但是,以下代码不起作用.可能是因为我真的不知道你是如何捕获异常的.

public override void Download()
{
    try
    {                                           
        var t = (ForumThread)Globals.Db.Thread.Get(_extIdForumThread, _idF);            
        try
        {
            throw new InvalidOperationException();
        }
        catch (InvalidOperationException exception)
        {
            return;
        }
        catch (Exception exception)
        {
            throw;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助都将非常感激.

c# exception-handling invalidoperationexception

1
推荐指数
1
解决办法
7637
查看次数

在循环期间修改集合时,它无法成为C#

下面的代码的foreach语句抛出InvalidOperationException,说"集合已被修改.枚举操作无法执行".我看不出这是怎么可能的,因为颜色在初始化后永远不会被修改.

Dictionary<Color, int> colorDictionary = new Dictionary<Color, int>();
//Put stuff in colorDictionary...
int currentBest = 257;
Color replaceColor = Color.Empty;
Dictionary<Color, int>.KeyCollection colors = colorDictionary.Keys;
foreach (Color c in colors)
{
    if (colorDictionary[c] == 0)
    {
        continue;
    }
    if (ColorDistance(color, c) < currentBest)
    {
        replaceColor = c;
        colorDictionary[c]--;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# collections invalidoperationexception concurrentmodification

1
推荐指数
1
解决办法
90
查看次数

“这意味着对Process.ExitTime的InvalidOperationException”该对象未启动进程,因此无法确定所请求的信息。“

我们有一个工具可以管理计算机的多个进程,并且在不确定的条件下会不时InvalidOperationException访问MSDN:Process.ExitTime属性。

异常消息说

该对象未启动处理,因此无法确定所请求的信息

这是否意味着我只能获得自己开始的进程的退出时间?那没有记录在任何地方。

.net process invalidoperationexception

1
推荐指数
1
解决办法
875
查看次数

WCF异常:无法添加到已知类型的列表中

今天是个好日子...

我收到一个意外的WCF错误,抱怨"已知类型",它们的名称相似,但在不同的名称空间中(请参阅下面的错误).

...请记住我还在试验这一切.

也…

我已经理解了常规Web服务的性质FORCES你要展平你的对象层次结构,但我读过的所有内容都表明你不必用WCF做到这一点......这就是为什么我要尝试这个.

所以我的问题是......

  • 问:是否有可能告诉WCF不要忽略名称间距?
  • 问:我错过了标签吗?不要将WCF标记(下面)考虑到名称间距吗?
  • 问:我是否必须压扁对象层次结构?

基本错误是......

类型'wcfFISH.StateManagement.Contractor.New'无法添加到已知类型列表中,因为已存在具有相同数据协定名称'wcfFISH.StateManagement.Contractor:New'的另一种类型'wcfFISH.StateManagement.CostDivision.New'.

错误看起来像......

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension: System.ServiceModel.Description.DataContractSerializerOperationBehavior
 contract: http://tempuri.org/:IFishData ----> System.InvalidOperationException: Type 'wcfFISH.StateManagement.Contractor.New' cannot be added to list of known types since another type 'wcfFISH.StateManagement.CostDivision.New' with the same data contract name 'wcfFISH.StateManagement.Contractor:New' is already present.
   at System.Runtime.Serialization.DataContract.CheckAndAdd(Type type, Dictionary`2 typesChecked, Dictionary`2& nameToDataContractTable)
   at System.Runtime.Serialization.DataContract.ImportKnownTypeAttributes(Type type, Dictionary`2 typesChecked, Dictionary`2& knownDataContracts)
   at System.Runtime.Serialization.DataContract.ImportKnownTypeAttributes(Type type) …
Run Code Online (Sandbox Code Playgroud)

wcf invalidoperationexception known-types

0
推荐指数
1
解决办法
4800
查看次数

跨线程操作在父控件中无效

我使用以下代码更新我的类中的标签,该标签扩展UserControl:

private delegate void LabelChanger(bool signedIn);

public bool SignedIn
{
    get { return _signedIn; }
    set
    {
        _signedIn = value;
        labelChanger(value);
    }
}

private void labelChanger(bool signedIn)
{
    if (label1.InvokeRequired)
    {
        BeginInvoke(new LabelChanger(labelChanger), signedIn);
    }
    label1.Text = signedIn ? "Sign Out" : "Sign In";
}
Run Code Online (Sandbox Code Playgroud)

问题是,当代码设置标签文本时,UI会更新,但是我在面板上得到一个"InvalidOperationException",它是标签的父级.有什么想法吗?谢谢.

c# user-interface invalidoperationexception winforms

0
推荐指数
1
解决办法
97
查看次数

位图区域已被锁定

string pathFile = @"C:\Users\user\Downloads\CaptchaCollection\Small\Sorted\";

var files = Directory.GetFiles(pathFile).Select(nameWithExtension => Path.GetFileNameWithoutExtension(nameWithExtension)).Where(name => { int number; return int.TryParse(name, out number); }).Select(name => int.Parse(name)).OrderBy(number => number).ToArray();
List<int> fileList = files.ToList();

image1 = new Bitmap(pathFile + fileList[0].ToString() + ".png");
image2 = new Bitmap(pathFile + fileList[1].ToString() + ".png");

if (compare(image2, image2))
{
    // if it's equal
    File.Delete(image2.ToString());
}
Run Code Online (Sandbox Code Playgroud)

所以基本上我现在所拥有的是每个文件都是数字的(没有扩展名).我创建了一个数组,然后将其转换为列表.

我使用我的全局变量image1,image2用于比较它们是否相同.

image1image2随着搜索的进行而改变.所以它改变了索引.

如果我的compare()方法返回true,它将删除第二个图像.

但是,在该compare()方法上,我似乎在此行上收到此异常错误:

BitmapData bmpData2 = bmp2.LockBits(rect, ImageLockMode.ReadOnly, bmp2.PixelFormat);
Run Code Online (Sandbox Code Playgroud)

c# bitmap invalidoperationexception

0
推荐指数
1
解决办法
6387
查看次数

为什么用此代码得到“ InvalidOperationException:无当前行”?

我正在尝试使用下面的GetInventoryRecordForUPC()方法从表中检索行。我可以通过输入检索LINQPad排精“ SELECT invName,line_id,ref_no,upc_code,说明,部门,VENDOR_ID,upc_pack_size,pack_size,ID,UNIT_COST,unit_list,unit_qty,NEW_ITEM从库存WHERE upc_code = '76145513' ”(中在我的测试方案中,“ upc”中传递的值是“ 76145513”)。

但是,当以下方法运行时,应用程序崩溃,并且日志文件包含:

System.InvalidOperationException: No current row
   at System.Data.SQLite.SQLiteDataReader.CheckValidRow()
   at System.Data.SQLite.SQLiteDataReader.VerifyType(Int32 i, DbType typ)
   at System.Data.SQLite.SQLiteDataReader.GetString(Int32 i)
   at HHS.TestHHSDBUtils.GetInventoryRecordForUPC(String upc)
Run Code Online (Sandbox Code Playgroud)

该方法将表的create语句注释掉,以显示列的名称和数据类型。

public Inventory GetInventoryRecordForUPC(String upc)
{
    ExceptionLoggingService.Instance.WriteLog("Reached 
TestHHSDBUtils.GetInventoryRecordForUPC");
    Inventory inv = new Inventory();
    using (SQLiteConnection conn = new 
SQLiteConnection(HHSUtils.GetDBConnection()))
    {
        conn.Open();
        const string qry = "SELECT invName, line_id, ref_no, upc_code, 
description, department, vendor_id, upc_pack_size, pack_size, id, unit_cost, 
unit_list, unit_qty, new_item FROM Inventory WHERE upc_code = @UPCCode";
        //CREATE TABLE Inventory(invName TEXT, line_id …
Run Code Online (Sandbox Code Playgroud)

c# sqlite invalidoperationexception windows-ce

0
推荐指数
1
解决办法
4025
查看次数