winForms, threading and dragdrop ?
From the attached code.With AllowDrop = True I get a .NET error as follows: System.InvalidOperationException: DragDrop registration failed. ---> System.Threading.ThreadStateException: The current thread must set to Single Thread Apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. From googling I need to set ApartmentState.STA. But this is readonly. How do I set STA correctly in Python for .NET? Any thoughts appeciated. Guy from CLR import System import CLR.System.Windows.Forms as WinForms from CLR.System.Drawing import Color, Size, Point from CLR.System import ComponentModel,Data, Collections, Threading from CLR.System.Runtime import InteropServices class dataNode(WinForms.TreeNode): def __init__(self,name,total,skill): self.Text = name self.Total = total self.Skill = skill class DnDTree(WinForms.Form): """A WinForms example. """ def __init__(self): self.components = System.ComponentModel.Container() self.treeView1 = WinForms.TreeView() self.listBox1 = WinForms.ListBox() self.SuspendLayout() # Set properties of TreeView 1. self.treeView1.AllowDrop = True self.treeView1.ImageIndex = -1 self.treeView1.Location = System.Drawing.Point(24, 32) self.treeView1.CheckBoxes = True self.treeView1.Name = "treeView1" self.treeView1.Nodes.Clear() self.treeView1.BeginUpdate() for pnode in range(50): self.treeView1.Nodes.Add(dataNode("node%d"%pnode,3,"high")) for cnode in range(15): self.treeView1.Nodes[pnode].Nodes.Add("childnode%d"%cnode) self.treeView1.EndUpdate() #for node in self.treeView1.Nodes: #print node.Total self.treeView1.SelectedImageIndex = -1 self.treeView1.Size = System.Drawing.Size(300, 600) self.treeView1.TabIndex = 0 #self.treeView1.MouseDown += System.Windows.Forms.MouseEventHandler(self.treeView1_MouseDown) #self.treeView1.AfterSelect += System.Windows.Forms.TreeViewEventHandler(self.treeView1_AfterSelect) #self.treeView1.QueryContinueDrag += System.Windows.Forms.QueryContinueDragEventHandler(self.treeView1_QueryContinueDrag) #self.treeView1.DragEnter += System.Windows.Forms.DragEventHandler(self.treeView1_DragEnter) #self.treeView1.ItemDrag += System.Windows.Forms.ItemDragEventHandler(self.treeView1_ItemDrag) # Set properties of ListBox 1. self.listBox1.AllowDrop = True self.listBox1.Location = System.Drawing.Point(24, 650) self.listBox1.Name = "listBox1" self.listBox1.Size = System.Drawing.Size(300, 300) self.listBox1.TabIndex = 1 #self.listBox1.DragDrop += System.Windows.Forms.DragEventHandler(self.listBox1_DragDrop) #self.listBox1.DragEnter += System.Windows.Forms.DragEventHandler(self.listBox1_DragEnter) #Now do Form self.AutoScaleBaseSize = System.Drawing.Size(5,13) self.ClientSize = System.Drawing.Size(400,1024) self.Controls.Add(self.listBox1) self.Controls.Add(self.treeView1) self.ShowInTaskbar = False self.StartPosition = WinForms.FormStartPosition.CenterScreen self.Name = "Form1" self.Text = "Form1" self.ResumeLayout(False) def Dispose(self): self.components.Dispose() WinForms.Form.Dispose(self) def main(): app = DnDTree() WinForms.Application.Run(app) app.Dispose() if __name__ == '__main__': main()
Hi Guy -- as far as I know, you can't change the thread state after any COM or interop calls have happened (and Python for .NET is all interop...). What you probably need to do is set the attribute in the C# 'embedding' application before initializing the python engine (or making any other interp calls). On Mon, 12 Sep 2005 15:38:55 +1200 Guy Robinson <guy@r-e-d.co.nz> wrote:
From the attached code.With AllowDrop = True I get a .NET error as follows:
System.InvalidOperationException: DragDrop registration failed. ---> System.Threading.ThreadStateException: The current thread must set to Single Thread Apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.
From googling I need to set ApartmentState.STA. But this is readonly. How do I set STA correctly in Python for .NET?
Any thoughts appeciated.
Guy
Cheers Brian. Interestingly the script runs OK from within PythonWin. I guess PythonWin must be setting STA correctly but the python console doesn't? Guy Brian Lloyd wrote:
Hi Guy -- as far as I know, you can't change the thread state after any COM or interop calls have happened (and Python for .NET is all interop...).
What you probably need to do is set the attribute in the C# 'embedding' application before initializing the python engine (or making any other interp calls).
On Mon, 12 Sep 2005 15:38:55 +1200 Guy Robinson <guy@r-e-d.co.nz> wrote:
From the attached code.With AllowDrop = True I get a .NET error as follows:
System.InvalidOperationException: DragDrop registration failed. ---> System.Threading.ThreadStateException: The current thread must set to Single Thread Apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.
From googling I need to set ApartmentState.STA. But this is readonly. How do I set STA correctly in Python for .NET?
Any thoughts appeciated.
Guy
-- A ReDGAS Project Robinson eDesign, P/F: +64 7 866 0626 Rings Beach, M: 021 238 0839 -ltd coverage RD2, E: guy@r-e-d.co.nz Whitianga. New Zealand The contents of this e-mail maybe CONFIDENTIAL OR LEGALLY PRIVILEGED, & is intended only for the persons named above. If this e-mail is not addressed to you, you must not use, read, distribute or copy this document. If you have received this document by mistake, please call us and destroy the original. Thank you
participants (2)
-
Brian Lloyd
-
Guy Robinson