|
Hi Everybody!
Man, what would I do without this board ?! I think everything I know in programming is because of everybody's help in this board and code project's articles! THANKS! (REALLY!)
anyway, enough being emotional and all .
I created a thread that receives a stream from the net to get a string.
I want that string to be displayed on the application textbox (or in other words, instant messanger).
The problem is that when I'm trying to assign StreamReader's text into textbox.text I'm getting exception saying that:
"Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on."
I REALLY don't understand why is that. is it because textbox1 is on ONE thread (the window form thread) and the StreamReader is on the other (the thread I created)?
if so, how can I fix it?!?!?!
That is the code of the service() method ran by the thread:
<br />
private void service()<br />
{<br />
while(true)<br />
{<br />
socket = listener.AcceptSocket();<br />
<br />
try<br />
{<br />
Stream stream = new NetworkStream(socket);<br />
<br />
StreamReader sr = new StreamReader(stream);<br />
<br />
StreamWriter sw = new StreamWriter(stream);<br />
sw.AutoFlush = true;<br />
<br />
string text = sr.ReadToEnd();<br />
this.textBox1.Text = text;<br />
<br />
stream.Close();<br />
}<br />
catch(Exception e)<br />
{<br />
<br />
MessageBox.Show(e.Message);<br />
<br />
}<br />
<br />
socket.Close();<br />
}<br />
}
Thanks!!!!
|
|
|
|
|
Green Fuze wrote: I REALLY don't understand why is that. is it because textbox1 is on ONE thread (the window form thread) and the StreamReader is on the other (the thread I created)?
Exactly. You're not allowed to access controls from a thread other than the one they were created on. Use Invoke or BeginInvoke to resolve the problem. There's also a very good article on this topic here on CP Search for Invoke and you should find it.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Thanks alot!
I'll try that.
by the way, I'm stealing your signature for my personal mail, ok? (its hilarious!)
|
|
|
|
|
Green Fuze wrote: I'm stealing your signature for my personal mail, ok?
Why wouldn't it be? It's a quote, so he "stole" it himself.
---
b { font-weight: normal; }
|
|
|
|
|
Hey
I am just starting with generics:
How can I convert a float to type T (which is either a float or a double and is iconvertible)?
I get the error:
Cannot convert type 'float' to 'T'
Problem 2...
The error:
Operator '*' cannot be applied to operands of type 'T' and 'T'
when I multiply two numbers
What can I do about this? Does float inherit from some class that defines basic mathematical operations?
All help is greatly appreciated
|
|
|
|
|
Perhaps a snippet or two of your code would help?
|
|
|
|
|
It's rather simple:
#1:
this.X = (T)xValue;
where this.X and xValue is float and T is double or float.
#2:
Math.Sqrt( X*X+Y*Y);
where X,Y are of type T which is either double or float (and impleements iconvertable).
thank you!
|
|
|
|
|
Hello
Actually this is not the snippet he was lokking for. You should post the definition of your generic class/method itself rather than just its call.
Also don't forget to check "Ignore HTML tags..." checkbox present below the editting textbox.
Regards
|
|
|
|
|
hi frens
i wan't to develop a software like circuitmaker, that's capable of making DFD's for that i need to implement drag and drop of objects , i have no idea how to make that happen , so that they can be drawn as well as scaled,etc. a solution with XML shall be a great help
Sameer Sood
|
|
|
|
|
Hello
ssoffline wrote: i need to implement drag and drop of objects
here take a look at these two articles:
How to tutorial (very good)[^]
TreeViewDragDrop[^]
ssoffline wrote: can be drawn as well as scaled
You can do all that using a Graphics object.
ssoffline wrote: a solution with XML shall be a great help
Now that's a bit weird!!
Regards
|
|
|
|
|
how can set print layout and zoom
by default as in this image
http://www.shoppinp.com/report.gif
??
Palestine
|
|
|
|
|
I'm developing a chart library with C# that outputs a SVG (Scalable Vector Graphic) file. SVG is pure XML output so there are no WinForms involved whatsoever.
In order to avoid overlapping text I'd like to use the MeasureString(...) and then set text properties (font size) accordingly, if overlap occurred. The Graphics object seems to be a prerequisite as it provides MeasureString(...). Is there a way of using MeasureString(...) functionality without using Graphics.
I've seen a possible solution which creates a random Graphics object out of a Bitmap which can then be "abused" to provide MeasureString(...) functionality:
<br />
Bitmap bmp = new Bitmap(1,1); <br />
Graphics g = Graphics.FromImage(bmp); <br />
g.MeasureString(...)<br />
Would you call this an elegant solution or is it just a workaround?
Once again: Is there another possibility of calculating the width of a string without Graphics?
Cheers.
|
|
|
|
|
Hello
I don't know about SVG formats but isn't displaying the text without overlapping would be the problem of the form that will read the file??
Anyway, if you want a graphics object, you don't need a bitmap, simple make call CreateGraphics() from your form -you still got one, right?-, and it will return a graphics object to work with.
Regards
|
|
|
|
|
Hello,
thanks for your reply.
Let me make things clearer.
SVG is a vector graphics format based on XML. The .NET application I'm currently developing generates such a SVG file. In this file various shapes, text elements, paths are described. Once the file is created the job is done.
As mentioned there are also text elements in the SVG, e.g. "This is a text" rendered as Arial, Bold, 12pt. MeasureString(...) would be ideal to calculate the size of any text although I'm eventually creating a SVG file.
Bear in mind: There is no form involved, so CreateGraphics() can't be invoked. Any further advice?
Thanks
|
|
|
|
|
Well, I guess then that you are developing a dll.
Even if there is no forms in your application, you can reference to System.Windows.Forms namespace and use TextRenderer class. There is a static method called MeasureText() that does the same thing.
Regards
|
|
|
|
|
You are right, I'm developing a dll.
Unfortunately, I'm using .NET Framework 1.1 - there isn't such a class available. Any other ideas?
Thanks.
|
|
|
|
|
|
Just tried .NET Framework 2.0 on a different machine. TextRenderer does it's job, thanks for your advice. Although the result is not that precise (same with MeasureString(...)) - I might provide my own method to measure strings. Anyway, I'll let you know!
Cheers.
|
|
|
|
|
Hi,
I have got the AppBar code running in a project sucessfully but the bar is always anchored to the top of my screen - I would like to have a button that will anchor the AppBar to the bottom of my screen on the taskbar.
I've been looking at the code and i'm not exactly sure what I have to change to make the AppBar locate to the bottom of the screen.
I was wondering if it was this section?
// Move and size the appbar so that it conforms to the
// bounding rectangle passed to the system.
MoveWindow(abd.hWnd, abd.rc.left, abd.rc.top, abd.rc.right - abd.rc.left, abd.rc.bottom - abd.rc.top, true);
Is there any chance someone could help me or point me in the correct direction on how I can do this?
Many Thanks,
Al
|
|
|
|
|
Hi all
I’ve created a simple windows service and successfully installed it using installutil.exe (.NET Framework 2.0 version). I want the service to run under the Local System account. When I attempt to start the service in SCM I get “Could not start the ATS Application Monitor service on Local Computer. Error 5: Access is denied” (where ATS Application Monitor is the name of the service). If I go to the Log On tab of the service properties dialog and change the logon account to my current user logon (which has full admin rights) I can then start the service.
The Startup Type to Automatic. I tried rebooting the machine in hopes that it would start before user account login.
The service was developed under Visual Studio 5 using the Windows Service template wizard but I built the installer class from the example code I was working from (Recipe 14.7 of Visual C# 2005 Recipes: A Problem-Solution Approach).
Thanks Leyla:
|
|
|
|
|
How to read and write in Notepad (.txt files).
Thanks.
Vasildb
|
|
|
|
|
Grab the mouse in your preferred mouse hand.
Move the cursor (white arrow thingy) towards the bottom left corner of the screen.
Click the "Start" button
Click the "Run" menu
Type in "Notepad"
Click OK
Click File | Open
Open the required file
Edit the file
Click File | Save
Click File | Exit
As of how to accomplish this, have you ever tried Google? Failing that try
|
|
|
|
|
|
The cat ate my mouse! What should I do now?
only two letters away from being an asset
|
|
|
|
|
Ah, now that's a problem. It's much too complicated for exceptional people to understand.
The hardest solution would be to use the keyboard (by pressing wierd combinations).
The simplest solution would be to go out and buy another computer with an RS232 port, setup a serial mouse and then using scripts in Hyperterminal or a similar program to provide the necessary signals to move the mouse.
As of how to accomplish this I wouldn't have a clue at the moment and I'm too lazy to google it
|
|
|
|