|
I have fixed the problem, but im not sure if there is a better way of doing it.
I ended up making a class that displays the ChatBox form I created as ShowDialog(), this seems to noew display the form, as Show() did not work. However, ShowDialog() blocks the thread untill that form is closed. So I have to craete the ChatBox form then pass it to an object that starts a new thread just for it. I dont really like this way because if i have 20 clients im chatting with thats 20 extra threads.
If anyone knows a better way please repsond. Sorry if my first post is a lot to take in, i didnt know how to explain my problem without giving all the background and details.
original problem, trying to be more clear:
a thread invokes an event, and a form (on the main thread) that is listening for the event creates a new form in the event hanlder, but the new form pops up and is "Not Responding".
I see that the new form is actually created in the thread that invokes the event, not the thread that has the form (that has the event handler), I dont really like that, i thought events you fire off and then the form thread would work on the event handler, i think this is why i had a problem (creating a form in a different thread and calling show() if i called showDialog() it works but the other thread blocks). I even tried async events, but even though its not the thread that invoked the event now creating the new form( working on the event handler), but its an anonymous new thread working on the event handler. I dont know how to make it more clear, this does sound confusing reading it back to myself sorry again.
The class to fix the problem, but creates a thread for each new form now:
private class EEChatBoxThread
{
internal EEChatBoxThread( FormEEChatBox chatBox )
{
_chatBox = chatBox;
Thread thread = new Thread( new ThreadStart( ShowChatBox ) );
thread.Name = chatBox.Client;
thread.IsBackground = true;
thread.Priority = ThreadPriority.BelowNormal;
thread.Start();
}
private void ShowChatBox()
{
_chatBox.ShowDialog();
}
private FormEEChatBox _chatBox;
}
Part of MainForm:
public void OnMessageReceived( Object sender, MessageReceivedEventArgs e )
{
FormEEChatBox chatBox = GetChatBox( e.From );
chatBox.MessageRecv( e.From, e.Message, e.Secure );
}
private FormEEChatBox GetChatBox( string with )
{
FormEEChatBox chatBox = ( FormEEChatBox )_chatBoxes[ with ];
// if the chatBox is not already on the screen, make one and display
if( chatBox == null )
{
chatBox = CreateChatBox( new string[] { with } );
}
return chatBox;
}
private FormEEChatBox CreateChatBox( string[] with )
{
FormEEChatBox chatBox = new FormEEChatBox( with, this );
chatBox.Closing += new CancelEventHandler( OnChatBoxClosing );
Monitor.Enter( _chatBoxes );
_chatBoxes.Add( with[ 0 ], chatBox );
Monitor.Exit( _chatBoxes );
// this class is above, just calls ShowDialog() on the chatBox form.
EEChatBoxThread boxThread = new EEChatBoxThread( chatBox );
return chatBox;
}
|
|
|
|
|
I think a better way might to be to use the InvokeRequired() and Invoke() methods in the OnMessageReceived method to marshal the event to the GUI thread. You should then be able to create and display the new form with no problems. See the article http://www.codeproject.com/cs/miscctrl/TSWizard.asp[^] for some examples of this.
Chris Jobson
|
|
|
|
|
Thanks you sooo much Chris Jobson, I looked at this sample you recomended the day you sent it, and see it was only the object "Step4" in that solution that i would be concerned with. This worked perfectly and i just summed up a quick way to handle what i was trying to do and how this helped me for others to see.
Again, thanks alot.
code below of what i basically was trying to do:
private void button1_Click(object sender, System.EventArgs e)
{
System.Threading.Thread formThread =
new System.Threading.Thread(
new System.Threading.ThreadStart( ShowNewFormThread ) );
formThread.IsBackground = true;
formThread.Priority = System.Threading.ThreadPriority.BelowNormal;
formThread.Start();
}
private delegate void ShowNewFormThreadDelegate();
private void ShowNewFormThread()
{
if( InvokeRequired )
{
ShowNewFormThreadDelegate del =
new ShowNewFormThreadDelegate( ShowNewFormThread );
Invoke( del, new Object [] { } );
return ;
}
++i;
Form form = new Form();
form.Text = i.ToString();
form.Width = 50;
form.Height = 50;
form.Owner = this;
_forms.Add( form.Text, form );
form.Show();
}
private void OnFormClosed( object sender, EventArgs e )
{
_forms.Remove( ( ( Form )sender ).Text );
}
int i = 0;
private Hashtable _forms = new Hashtable();
|
|
|
|
|
Dear experts,
Does anyone know how to get access to the text box control that is displayed underneath icons on the Windows desktop. I want to re-use this text box in my own application as it has the "growth" behaviour that I urgently need. I need a text box that will grow in height as more text is added to it.
PLEASE HELP ME! Thanks in advance for your assistance.
Benjamin
|
|
|
|
|
I think you asked a similar question previously, which i answered for you earlier today. Please drop me an e-mail if you can't find it / need clarification
|
|
|
|
|
Is there a way to create a “COM - Out Of Process” style object in C# (.NET)without using .NET remoting?
Any ideas out there? Am I stuck with remoting no matter what?
Thanks!
|
|
|
|
|
Hi,
Is there an activex control for importing flash(swf)? or How can i import a swf into my app???
|
|
|
|
|
In a C# Windows Form, I have a treeview/listview pair that behaves like Windows Explorer, displaying drives, folders and files... I want to display certain treeview nodes as disabled or greyed...
Can someone tell me how this can be done?
thanks very much.
|
|
|
|
|
A simple solution that may do what you want is to simply set the ForeColor of the node you want greyed to Color.Gray or Color.LightGray . You can then handle the BeforeSelect event of the treeview and check for the fore color, if it is Color.Gray or Color.LightGray , depending on which you choose, set the Cancel property of the TreeViewCancelEventArgs to true:
private void treeView1_BeforeSelect(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
{
if(e.Node.ForeColor == Color.Gray)
{
e.Cancel = true;
}
}
Hope this is useful
|
|
|
|
|
Yo Don,
thanks for the tip, that will do nicely...
|
|
|
|
|
Hello all,
I have just created my first C# exe. The exe uses the System.Process namespace to launch another exe (say c:\\X.exe - that is the X.exe is on the c: drive) . I compile my exe and it works fine on my machine. But when I put my exe on the network (on a mapped drive) - I get the following error message when I try to click on the button that launches the X.exe, I get the dialog box with the error message -
"The application attempted to perform an operation not allowed by the security policy. The operation required the SecurityException. To grant this application the required permission please contact the system administrator, or use the Microsoft .Net security policy administration tool."
I tried building my application on the same mapped network drive and running it through .Net and I get the dialog box with the following message -
"The project location is not fully trusted by the .Net runtime. This is usually because it is either a network share or mapped to a network share not on the local machine. If the output path is under the project location, your code will not execute as fully trusted and you may receive unexpected security exceptions."
Which is pretty much what happens.
Has anyone run into this problem before. Any ideas how I can fix this?
Thanks in advance.
|
|
|
|
|
This is the intended behavior, and was done to account for situations where you can't trust computers on your LAN (college dorms for example).
To change this, run the "Microsoft .NET Configuration Wizards" applet, on 2K/XP this is in the Administrative Tools portion of the Control Panel. Not sure where this is on Win98.
If you can't trust your LAN, then use the "Trust an Assembly" wizard to give your application full trust. If you CAN trust your LAN then you can use the "Adjust .net Security" wizard to adjust the LAN to Full Trust.
James
"It is self repeating, of unknown pattern"
Data - Star Trek: The Next Generation
|
|
|
|
|
I was wondering If anyone had an experience with retrieving path of the Windows directory C#
Unfortunately, Directory namespace has no class/member function to retrieve this information,
And when I try to use
this piece of code to import SDK function:
[DllImport("kernel32")]<br />
private static extern uint GetWindowsDirectory(string lpBuffer,
uint uSize
);
But apperantly I couldnt make this work.
Basically lpBuffer should be ref parameter in C#, but in decleration of this function is LPSTR in SDK,
Any help will be appreciated,
Thanks,
~Mithat
|
|
|
|
|
System.Enviroment
I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02
|
|
|
|
|
I already checked the System.Environment
And it does not have a memeber variable to rerieve the path for Windows directory .
I want to retrieve c:\winNT
not C:\winNT\system32 (which you can retrieve with Environment.SystemDirectory() )
or any of those: Program Files, Programs, System, or Startup directory (which you can retrieve with Environment.GetFolderPath() )
is there any method in System.Environment that rerieves Windows Directory path ?
Thanks
~Mithat
|
|
|
|
|
Mmithat wrote:
not C:\winNT\system32
just remove the \system32 bit or \system in win9x....
I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02
|
|
|
|
|
this is how i retrieve it right now,
csTempDirectory = Environment.SystemDirectory;<br />
m_csWindowsDirectory = Directory.GetParent(csTempDirectory).ToString();
But I dont want to rely on those kinda stuff
sometimes system does not let u get those paths.
and it may vary .
in my orginal message, there should be a way to import this function (getwindowsdirectory()) from SDK
But i am just not comfortable with the variables in import-declaration-functions.
~Mithat
|
|
|
|
|
Or you can try:
Environment.GetEnvironmentVariable("windir");
I havent tried it, but it should work
I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02
|
|
|
|
|
I like that better.
Thanks =)
~Mithat
|
|
|
|
|
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
private static extern uint GetWindowsDirectory(
[MarshalAs(UnmanagedType.LPTStr)]
System.Text.StringBuilder lpBuffer,
uint uSize
);
System.Text.StringBuilder buffer = new System.Text.StringBuilder(255);
GetWindowsDirectory( buffer, (uint) buffer.MaxCapacity ); Works here.
I think as a general rule, when you need to pass in a buffer to receive text you use a StringBuilder object.
[edit]Looking at MSDN for the GetWindowsDirectory function it is supposed to accept a TCHAR, so the MarshalAs and DllImport attributes should be changed accordingly as above.[/edit]
James
"It is self repeating, of unknown pattern"
Data - Star Trek: The Next Generation
|
|
|
|
|
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]private static extern uint GetWindowsDirectory( [MarshalAs(UnmanagedType.LPTStr)] System.Text.StringBuilder lpBuffer, // buffer for Windows directory uint uSize // size of directory buffer);
Really great language C#. Fast, simple and very comfortable...
|
|
|
|
|
Better get used to it.
|
|
|
|
|
i'm curently writing an application that use winamp , but i can't find any way that i can get informaions about the song it's curently playng(i'm especialy interest in geting the path of the file)
sorry for my english
|
|
|
|
|
I'd like to create an array of references to variables--for example:
void Foo()
{
int a;
string s;
Point p;
Item[] items=new Item[] {new Item(out a), new Item(out s), new Item(out p)};
then pass the array to a function and have the function iterate through the array and populate the data, so that the local vars in the caller get populated--for example:
...
PopulateItems(items);
...
}
void PopulateItems(Item[] items)
{
items[0]=1;
items[1]="abc";
items[2]=new Point(0, 0);
}
Keep in mind this is a simplified example, as ultimately the thing needs to extract the type. The point is I'm looking for something that does this given a dynamic list of items.
Is this possible in C# (and how), the equivalent in C++ being int*, string*, and Point* ?
Thanks!
Marc
Help! I'm an AI running around in someone's f*cked up universe simulator. Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus Every line of code is a liability - Taka Muraoka Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files"
|
|
|
|
|
Marc, how does your poor girlfriend/wife keep up with this?
I'm not really grasping what you are trying to do, but I have a suggestion or 2. Have you tried this in an unsafe context, that would appear to be the easiest. Secondly, boxing those variables mite do the trick, but I'm not really sure about that.
Hope you win
I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02
|
|
|
|