尝试更改方法名称也尝试创建新类并将form1中的代码放在那里,但没有任何效果。
这是form1中的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VirtualBox;
using System.Runtime.InteropServices;
namespace Desktop
{
public partial class Form1 : Form
{
#region DLLs
[DllImport("user32.dll")]
private static extern IntPtr CreateDesktop(string lpszDesktop, IntPtr lpszDevice, IntPtr pDevmode,
int dwFlags, long dwDesiredAccess, IntPtr lpsa);
[DllImport("user32.dll")]
private static extern bool SwitchDesktop(IntPtr hDesktop);
[DllImport("user32.dll", EntryPoint = "CloseDesktop", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseDesktop(IntPtr handle);
[DllImport("user32.dll")]
public static extern bool SetThreadDesktop(IntPtr hDesktop);
[DllImport("user32.dll")]
public static extern IntPtr GetThreadDesktop(int dwThreadId);
[DllImport("kernel32.dll")]
public static extern int GetCurrentThreadId();
#endregion
#region Enumeratoren
[Flags]
internal enum DESKTOP_ACCESS_MASK : uint
{
DESKTOP_NONE = 0,
DESKTOP_READOBJECTS = 0x0001,
DESKTOP_CREATEWINDOW = 0x0002,
DESKTOP_CREATEMENU = 0x0004,
DESKTOP_HOOKCONTROL = 0x0008,
DESKTOP_JOURNALRECORD = 0x0010,
DESKTOP_JOURNALPLAYBACK = 0x0020,
DESKTOP_ENUMERATE = 0x0040,
DESKTOP_WRITEOBJECTS = 0x0080,
DESKTOP_SWITCHDESKTOP = 0x0100,
GENERIC_ALL = (DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU |
DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK |
DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP),
}
#endregion
public Form1()
{
InitializeComponent();
Desktop("MyDesktop");
System.Threading.Thread.Sleep(1000);
Application.DoEvents();
Desktop();
Application.DoEvents();
System.Threading.Thread.Sleep(3000);
SwitchToOrginal();
Application.DoEvents();
}
public void Dispose()
{
SwitchToOrginal();
((IDisposable)this).Dispose();
}
/// <summary>
/// Unterklassen können hier die Funktionalität der Objektzerstörung erweitern.
/// </summary>
/// <param name="fDisposing"></param>
protected virtual void Dispose(bool fDisposing)
{
if (fDisposing)
{
// Hier die verwalteten Ressourcen freigeben
//BspVariable1 = null;
CloseDesktop(DesktopPtr);
}
// Hier die unverwalteten Ressourcen freigeben
}
void IDisposable.Dispose()
{
Dispose(true);
GC.SuppressFinalize(this); //Fordert das System auf, den Finalizer für das angegebenen Objekt nicht aufzurufen
}
#region Variablen
IntPtr _hOrigDesktop;
public IntPtr DesktopPtr;
private string _sMyDesk;
public string DesktopName
{
get
{
return (_sMyDesk);
}
set
{
_sMyDesk = value;
}
}
#endregion
#region Konstruktoren
public void Desktop()
{
_sMyDesk = "";
}
public void Desktop(string sDesktopName)
{
_hOrigDesktop = GetCurrentDesktopPtr();
_sMyDesk = sDesktopName;
DesktopPtr = CreateMyDesktop();
}
#endregion
#region Methoden
public void show()
{
SetThreadDesktop(DesktopPtr);
SwitchDesktop(DesktopPtr);
}
public void SwitchToOrginal()
{
SwitchDesktop(_hOrigDesktop);
SetThreadDesktop(_hOrigDesktop);
}
private IntPtr CreateMyDesktop()
{
return CreateDesktop(_sMyDesk, IntPtr.Zero, IntPtr.Zero, 0, (long)DESKTOP_ACCESS_MASK.GENERIC_ALL, IntPtr.Zero);
}
public IntPtr GetCurrentDesktopPtr()
{
return GetThreadDesktop(GetCurrentThreadId());
}
#endregion
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
错误位于此行的form1.designer.cs中:
protected override void Dispose(bool disposing)
尝试在此处和form1中更改方法名称,但它不起作用。
答案 0 :(得分:7)
Neel是对的,它是偏袒的,
如果你需要在处理时做一些工作,你可以使用一些事件,如OnDispose,Dispose + = ...等。配置功能很奇怪。在设计器文件中自动生成。即使您更改设计器文件,它也会再次生成。因此您无法覆盖此重载方法。只是你可以像这样调用它.Dispose(true); ...
如果您要处置其他内容,请使用此
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Disposed += (s, a) =>
{
//Dispose unmanaged stuffs etc.
};
}
顺便说一下,我在删除designer.cs方法后测试过,不会导致Windows Forms应用程序重新生成。所以有可能是sol。同样。当事情发生变化时,大多数代码生成器都会替换设计器文件。比如EntityCode生成器,SL生成代码。
答案 1 :(得分:4)
已经有一个名为Dispose的方法,检查设计器生成的代码。
从form1.cs文件中删除Dispose ...您是否注意到公共部分类行?这意味着该类在2个或更多文件中传播。所以Dispose已经在form1.designer.cs中定义。所以不要创建另一个Dispose,或者如果你真的必须删除“partial”。