|
You posted whilst I was composing the message below! Hope it helps anyway
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Reading through the messages above, you don't seem to have grasped the 'unsubscribe from event' way of dealing with this.
If you make sure that your thread updates the UI only via an event then by unsubscribing to that event, the UI update code will not get called! All the InvokeRequired stuff should be in the event consumer's handler method.
It's not clear whether you wish to let the thread complete naturally after the application closes or terminate it immediately. By setting IsBackground to true , it will terminate. Not setting (or setting to false ) will allow it to continue.
Here's some demo code which may help explain!
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows.Forms;
namespace ThreadingDemo
{
public partial class FormMain : Form
{
MyThreadedClass myThreadedClass;
public FormMain()
{
InitializeComponent();
myThreadedClass = new MyThreadedClass();
myThreadedClass.GotDataCompleted += myThreadedClass_GotDataCompleted;
FormClosing += FormMain_FormClosing;
Shown += new EventHandler(FormMain_Shown);
}
void FormMain_Shown(object sender, EventArgs e)
{
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
myThreadedClass.GotDataCompleted -= myThreadedClass_GotDataCompleted;
}
private void myThreadedClass_GotDataCompleted(object sender, GotDataCompletedEventArgs e)
{
if (InvokeRequired)
Invoke(new MethodInvoker(delegate { myThreadedClass_GotDataCompleted(sender, e); }));
else
{
foreach (string item in e.Data)
Text = item;
}
}
private void RunThread()
{
myThreadedClass.Begin();
}
private void RunThreadAsBackground()
{
myThreadedClass.SetAsBackground();
myThreadedClass.Begin();
}
}
public class MyThreadedClass
{
public event EventHandler<GotDataCompletedEventArgs> GotDataCompleted;
private Thread thread;
public MyThreadedClass()
{
thread = new Thread(new ThreadStart(Start));
}
public void Begin()
{
thread.Start();
}
protected virtual void OnGotDataCompleted(GotDataCompletedEventArgs e)
{
EventHandler<GotDataCompletedEventArgs> eh = GotDataCompleted;
if (eh != null)
eh(this, e);
}
private void Start()
{
GotDataCompletedEventArgs e = new GotDataCompletedEventArgs();
e.AddData("Data 1");
Thread.Sleep(10000);
e.AddData("Data 2");
OnGotDataCompleted(e);
}
public void SetAsBackground()
{
thread.IsBackground = true;
}
}
public class GotDataCompletedEventArgs : EventArgs
{
private List<string> data;
public GotDataCompletedEventArgs()
{
data = new List<string>();
}
public ReadOnlyCollection<string> Data
{
get { return data.AsReadOnly(); }
}
internal void AddData(string item)
{
data.Add(item);
}
}
}
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
The line in my form is flickering.
The form is DoubleBuffered which made me kind of lost .
Help?
using System;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Design;
namespace Threading
{
public partial class Form1 : Form
{
Pen pen = new Pen(Color.Black);
Thread t;
public Form1()
{
InitializeComponent();
pen.Width = 3;
pen.StartCap = System.Drawing.Drawing2D.LineCap.SquareAnchor;
pen.EndCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
t = new Thread(new ParameterizedThreadStart(Commando));
t.Start(true);
}
public void Commando(object obj)
{
while (true)
{
Graphics x = this.CreateGraphics();
x.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
x.DrawLine(pen, new Point(0, 0),
new Point(
MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y));
this.Refresh();
}
}
}
}
Any help is appreciated.
|
|
|
|
|
nbgangsta wrote: this.Refresh();
This will not work, you are trying to refresh the form from a different thread. It will throw a cross thread reference error.
nbgangsta wrote:
t = new Thread(new ParameterizedThreadStart(Commando)); <br />
t.Start(true);
Not sure what you are trying to do. But I would override the form's OnPaintMethod and use the MouseMove event to draw the line rather than using a different thread to draw it.
Tarakeshwar Reddy
There are two kinds of people, those who do the work and those who take the credit. Try to be in the first group; there is less competition there. - Indira Gandhi
|
|
|
|
|
Flickering is a sign that it is not doublebuffered or you are drawing on the wrong surface or flipping too soon.
Double buffering is:
Create a primary surface with one backbuffer (essentially 2 display surfaces)
Always draw on the backbuffer (it is not visible) and when all drawing is complete swap the 2 surfaces.
the swapping makes the backbuffer the primary and the primary becomes the backbuffer
this is what prevents flickering, the instantanious swap of the source display surface.
I haven't used the above drawing methods only DirectX and some GDI so i could be totally wrong about your code.
It appears you are creating/initializing your drawing surfaces (this.CreateGraphics()) repeatedly....this should only be done once.
- Where does it create your drawing surface Doublebuffered?
- How does x.DrawLine know which surface of X to draw on?
I also see "new" several times within you while(true) loop but no "delete" unless their is some sort of garbage collection for memory eventually you'll run out.
Another issue you could have is since the drawing is in it's own thread....when the main form/window needs to refresh, it needs to know what to draw (the primary surface) or let the drawing thread know it needs to refresh the display area that it is handling.
Hope this helped.
|
|
|
|
|
I can't say for sure what's causing it, but I was curious about something in your code:
public void Commando(object obj)
{
while (true)
{
Graphics x = this.CreateGraphics();
x.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
x.DrawLine(pen, new Point(0, 0),
new Point(
MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y));
this.Refresh();
}
}
you set it up as a parameterized void , and you passed it true, but then you never actually check the object. obj does absolutely nothing in this example.
I don't think you're going to be able to get away from the flicker the way you're doing it.
Why, can't you just put the code into the MouseMove? It will flicker a tiny bit while you're moving, but when the mouse is stopped, no flicker.
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.Refresh();
Graphics x = this.CreateGraphics();
x.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
x.DrawLine(pen, new Point(0, 0),
new Point(
MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y));
}
Oh, and I modified your code to be more thread friendly:
public void Commando(object obj)
{
while (true)
{
Graphics x = this.CreateGraphics();
x.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
x.DrawLine(pen, new Point(0, 0),
new Point(
MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y));
if (this.InvokeRequired)
{
RefreshMe = RefreshForm;
this.Invoke(RefreshMe);
}
}
}
private delegate void RefreshFormDel();
private RefreshFormDel RefreshMe;
private void RefreshForm()
{
this.Refresh();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (t != null)
if (t.IsAlive)
t.Abort();
}
|
|
|
|
|
Am not sure how can I describe it or what does Microsoft call it but listen...
When you start Windows Vista and you get the login screen it will show the user picture and the password textbox with the work password in light color so when you click inside it will disappear then whenyou clear the text it will show again..
first, what do you call it please.
then how can I do it for both user id and password textbox?
|
|
|
|
|
It's easy enough to crete your own custom TextBox control that does this. There several different ways of doing it, but they all start with creating a seperate class that inherits from TextBox and probably provides some custom painting code and a couple of extra properties to show what you want light-shaded if the Text property is an empty string AND the control does not have the focus.
|
|
|
|
|
but does this thing has a name so I can at least get guide from internet?
|
|
|
|
|
|
|
I have a network application built using C# and for the most part 99% of my users have no problems with it.
I have a few users that don't have IE installed and are using Firefox as their main browser and they have reported strange behavior in my application. One user was savvy enough to install IE and saw the display issues went away. This puzzled me since I would assume when you distribute a .NET 3.5 application that it would install all of the required components. Why would an installation of IE 7 or 8 improve the look inside my application? More importantly, why is the application not working properly without IE installed. I guess that is the bigger question here. Also, I have users telling me they can't click buttons and such.
I am puzzled by this since it is merely a browser control. They should be able to click buttons and navigate as they wish. 99% of my user base is funtioning flawlessly, so I know it is not the application, but the environment.
Any ideas where to look? The WebControl seems to be part of the 2.0 Framework.
|
|
|
|
|
Your users mostly likely have an older version of IE that was preinstalled on the machine.
The .net framework and IE are two separate things, just because you install the framework components you don't automatically get the latest IE. The WebBrowser control will use whatever version of IE is available.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
WebBrowser Control uses ActiveX plugin, which is not by default on firefox, so it will not behave the same way.
You can install the add-on to firefox for same.
some example of such add-ons are:
1) neptune
See for more info http://www.meadroid.com/neptune/index.htm
2) To use ActiveX controls in Firefox 3, see this extension:
http://code.google.com/p/ff-activex-host/
Ravie Busie
Coding is my birth-right and bugs are part of feature my code has!
_________________________________________
Me
Facebook
Twitter
|
|
|
|
|
Thank you for the information. You are correct! Now I have a more interesting question. The WebBrowser object is installed with the suite of office products and I found a link on the microsoft site that allows people to install those objects, but when they click the link it says they must have MS office installed.
So how do I take the Active-X control my application was built on and distribute it? It is a network application and I need to be able to distribute everything the user needs to run, but I can't force people to go buy office.
Any thoughts on that?
|
|
|
|
|
Jon Braunsma wrote: So how do I take the Active-X control my application was built on and distribute it? It is a network application and I need to be able to distribute everything the user needs to run, but I can't force people to go buy office.
You don't. All they need is the install of Internet Explorer that your site uses to look it's best. If that's IE7, then you can provide the link to install IE7. They do not need any part of Office to use the WebBrowser control.
|
|
|
|
|
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
public class test
{
string strID;
public string hello()
{
strID= this.txtTextBox1.Text; //here i am confused with my basic concept (can i assign value to strID from TextBox and if yes then how to get TextBox Value from form2 because I am making a new class here)
return strID;
}
}
}
|
|
|
|
|
Hi,
You can use C# Properties for this... Here's what u want...
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
test _test = new test();
string strId = _test.StrID;
}
}
public class test
{
string strID = string.Empty;
public string StrID
{
get { return strID; }
set { strID = value; }
}
public string hello()
{
strID = this.txtTextBox1.Text;
return strID;
}
}
Thanks,
Ram
|
|
|
|
|
Properties work for getting information from an object instance you hold a reference to.
To get the object to tell the 'parent' without it being requested, use events - see here[^].
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
how to retrive Columns name with each row with sql query....
suppose i have 10 rows in a table........
|
|
|
|
|
Please move this message to the General Databases forum.
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.
|
|
|
|
|
|
Your question isn't very clear.
When using a DataReader you can use IDataReader.GetSchemaTable to get information about the columns retrieved.
|
|
|
|
|
Hi,
I wonder if there is practically any difference between the two options of declaring a variable in C#, listed below;
1)
int i;
2)
int i = new int();
Thanks..
|
|
|
|
|
no difference, except for typing effort.
|
|
|
|