|
FYI, that would prevent a locking situation, but would not likely prevent a cross-thread exception. It would also change the flow of the program (i.e., the call would be asynchronous rather than blocking), which the OP may not want.
|
|
|
|
|
I agree.
|
|
|
|
|
Without seeing your code, it's pretty much impossible to tell you where it's going wrong.
|
|
|
|
|
Code is the following:
Delegate:
private delegate void ChangeTextDelegate(string text);
Method:
public static void ChangeText(string text)
{
if (richtextbox1.InvokeRequired)
{
richtextbox1.Invoke(new ChangeTextDelegate(ChangeText), new object[] { text });
}
else
{
int startIndex;
startIndex = richtextbox1.TextLength; <- Exception points here
...
}
}
I call it like this: ChangeText("hello"); (in another thread)modified on Thursday, March 18, 2010 7:19 AM
|
|
|
|
|
Hi,
please show code inside PRE tags, that results in better readability.
What you have shown seems correct, assuming the RTB got created on the GUI thread. It would be wrong when created e.g. inside the DoWork handler of a BackgroundWorker. How and where did you create it?
|
|
|
|
|
FYI, this is what code looks like when you wrap it in PRE tags and specify C# for the LANG attribute, as Luc mentioned:
private delegate void ChangeTextDelegate(string text);
public static void ChangeText(string text)
{
if (richtextbox1.InvokeRequired)
{
richtextbox1.Invoke(new ChangeTextDelegate(ChangeText), new object[] { text });
}
else
{
int startIndex;
startIndex = richtextbox1.TextLength;
}
}
Not sure why you are getting an exception there (perhaps post a screenshot and the full text of the exception?), but you might try the following to see if it works (changes in bold):
private delegate void ChangeTextDelegate(string text);
public static void ChangeText(string text)
{
if (<big><big>this</big></big>.InvokeRequired)
{
<big><big>this</big></big>.Invoke(new ChangeTextDelegate(ChangeText), new object[] { text });
}
else
{
int startIndex;
startIndex = richtextbox1.TextLength;
}
}
FYI, here is what I typed to make the code look like it does above:
<pre lang="C#">// Delegate:
private delegate void ChangeTextDelegate(string text);
// Method:
public static void ChangeText(string text)
{
if (<b><big><big><u>this</u></big></big></b>.InvokeRequired)
{
<b><big><big><u>this</u></big></big></b>.Invoke(new ChangeTextDelegate(ChangeText), new object[] { text });
}
else
{
int startIndex;
startIndex = richtextbox1.TextLength; // <- Exception points here.
// ...
}
}</pre>
|
|
|
|
|
Here is the stack trace, perhaps it can help clarify the problem...
System.InvalidOperationException was unhandled
Message="Cross-thread operation not valid: Control 'SomeClass' accessed from a thread other than the thread it was created on."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.get_InternalHandle()
at System.Windows.Forms.Control.WmWindowPosChanged(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.RichTextBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmCreate(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.RichTextBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.IntCreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
at System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.TextBoxBase.CreateHandle()
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.RichTextBox.get_TextLength()
at SomeNamespace.SomeClass.Changetext(String text, Color color) in C:\...\SomeClass.cs:line 827
at SomeNamespace.SomeClass.SomeThreadFun() in C:\...\SomeClass.cs:line 112
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
|
|
|
|
|
Soo, any ideas?
|
|
|
|
|
I assume you already tried using the "this" keyword like I said above? If that doesn't work, you can take two approaches. With one approach, try to create the error with a very small tester form. Then, post that here for us to review. Or, try removing code from your project little by little until it starts working.
|
|
|
|
|
I am currently testing it with the 'this' keyword. But why should this approach solve the problem? What are the technicalities behind it?
|
|
|
|
|
Was just a wild guess really. Not sure if it would help.
|
|
|
|
|
Okay, that didn't help Any more ideas?
|
|
|
|
|
Show me the exact code that causes the problem. Create as small of a tester application as you can and paste ALL of the code for it (but remember to try and make the example concise). It's probably something simple, but I can't help you unless I see the code you are using.
|
|
|
|
|
I was finally able to fix the problem:
Apparently the handle for the richtextbox did not exist when i tried to invoke it. Since i used it in a background thread, the invokerequired property returned 'false', sometimes causing it to display the illegal cross thread operation exception (suring program startup).
The solution was to use the Form_load event which would signal the background thread when it was fully loaded.
But its still a bit unclear how such a thing can happen. When does actually InitializeComponent return? When can i be sure that the control handles exist?
|
|
|
|
|
Glad to hear you got it working.
InitializeComponent should be called from the constructor. You should only reference controls after that call to InitializeComponent.
|
|
|
|
|
Oh but i did call it from the constructor, then started the background thread and inside that thread tried to access the controls.
|
|
|
|
|
I'm creating a control where I want 3 separate containers(2 sub containers and 1 main container)
So to do that, I simply inherit the Panel control and adding 2 sub Panels in Constructor.
And the problem is, when I drag any item in that control it get added to the Main container...but I want it to be inside the Panel it dragged on.
It suppose to be like TableLayoutPanel
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
You can edit the InitializeComponent() function afterwards manually. Search for a line like
this.Controls.Add(this.controlWhichShouldBeOnPanel2);
and change that to something like
panel2.Controls.Add(this.controlWhichShouldBeOnPanel2);
|
|
|
|
|
Thats not what I want. I want design-time support.
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
The problem is prossibly because the controls were added in the constructor, not via the designer. This means the size and postion of the sub-panels might not be visible in the designer. One method I sometimes use is to temporarily set the background colours of your panels to various (and obviously different) colours, so you can see what is where in the designer. Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
|
|
|
|
|
nope, I can see them clearly
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
I have this xml:
<resume>
<details>
<resumeName>Jassim</resumeName>
</details>
<experience>
<company ID="xxxxxxxxxxxxxxxx">
<name>apple</name>
<title>developer</title>
</company>
<company ID="bbbbbbbbbbbbbbbbb">
<name>microsoft</name>
<title>analyst</title>
</company>
</experience>
</resume>
how can I bind the expreience elemnt into a data grid so I will have ID, company and title columns?
|
|
|
|
|
I would create a new class with your ID, company, and title fields and map each company XML node to an instance of that class. Then bind that collection of class instances to a DataGrid. You can also just load company XML node fields into a DataTable in the same way that OriginalGriff showed you how to load FileInfo objects in response to your other post.
You can use XPath or Linq2XML to get the company nodes from the XML document.
|
|
|
|
|
How can I populate list if files with size and created date into a data table then bind to a dataGrid?
|
|
|
|
|
This uses a DataGridView rather than the web-centric DataGrid, but the principle is the same. (I can't be bothered to load a web site to test it, so you get a DataGridView).
DataTable dt = new DataTable("Directory of " + strPath);
dt.Columns.Add("Name");
dt.Columns.Add("Size");
dt.Columns.Add("CreateDate");
DirectoryInfo di = new DirectoryInfo(strPath);
FileInfo[] files = di.GetFiles();
foreach (FileInfo fi in files)
{
dt.Rows.Add(fi.Name, fi.Length ,fi.CreationTime);
}
dataGridView1.DataSource = dt;
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|