|
hi all,
How to switch between two threads ?
I am having two threads first one is main thread and second one a thread in which a function is called.In second thread,while loop is there.When one iteration is completed in loop i have to switch to main thread and call a function and again come to second thread and repeat the process.
Please help me..
|
|
|
|
|
praveen pandey wrote: I am having two threads first one is main thread and second one a thread in which a function is called.In second thread,while loop is there.When one iteration is completed in loop i have to switch to main thread and call a function and again come to second thread and repeat the process.
You have the threads signal each other when control should pass from one to the other. While one thread is doing work, the other waits. When the work is finished, the working thread signals the waiting thread that it's done. It then begins waiting while the other thread does its work.
Typically, a worker thread looks something like this:
private void ThreadMethod()
{
lock(lockObject)
{
while(someConditionIsTrue)
{
Monitor.Wait(lockObject);
}
}
}
From another thread, you have to pulse the lock to get the worker thread running:
lock(lockObject)
{
Monitor.Pulse(lockObject);
}
However, since one of these threads is the main thread, you probably don't want it blocking while the other thread is doing work. This could tie up your UI. So we need more information about the kind of application you're writing. Is it a console app? A Window Forms app?
|
|
|
|
|
It's a windows app.
Here is scenario.
secondThreadMethod()
{
while(conditonIsTrue)
{
some code;
//After one iteration i want to come out(since conditon is still true) and goto main thread finish some work and come back to this thread and continue in this way.
}
}
thanks
|
|
|
|
|
praveen pandey wrote: It's a windows app.
Ok, well in your thread loop, you could marshal a delegate invocation to the main thread:
private void ThreadMethod()
{
lock(lockObject)
{
while(someConditionIsTrue)
{
Monitor.Wait(lockObject);
BeginInvoke(new WorkDelegate(DoWorkOnMainThread));
}
}
}
private delegate void WorkDelegate();
private void DoWorkOnMainThread()
{
lock(lockObject)
{
Monitor.Pulse(lockObject);
}
}
This assumes you're writing a Forms application. The idea is that you have the main thread running and not blocking while the worker thread does its work. Then when you need work done on the main thread, the worker thread marshals a delegate invocation that does the work to the main thread through BeginInvoke . The method on the main thread signals the worker thread when it's done so that the worker thread can continue for another iteration.
|
|
|
|
|
Thanks alot.
It really worked in the way what i wanted.
Thanks Again...
|
|
|
|
|
Hi,
How to bypass character in TextBox, so when we press alphabet keyboard key will bypass it. But when we press number TextBox will confirm this input.
Thanx in advanced
Regard,
Edwin
|
|
|
|
|
private void txtBox__KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar))
{
e.Handled = true; // input is not passed on to the control(TextBox)
}
}
|
|
|
|
|
Thanx a lot -Bobo
God Bless U
Regard,
Edwin
|
|
|
|
|
You need to do this:
if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar)
or otherwise your arrow/delete/etc keys will not work.
edwin46 wrote: There is no KeyChar properties in e
It is constantly depressing to me, the inability of so many people to use intellisense, or google. Goodness knows what you're doing, but the KeyChar property is most certainly present.
http://msdn2.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.handled.aspx[^]
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
write your code in key press event
|
|
|
|
|
Sorry Guys,
It's working 100%
Regard,
Edwin
|
|
|
|
|
I want to bind a table in my access database to a datagridview control in my application. I have created a dataset, and bind the table successfully to my datagridview. Now I want to add two new columns to the table in database. I have updated the data table of both dataset and access database, however, the changes are not reflected in the datagridview, as I added two new columns into datagridview, I can not see the two fields I just added to the dataset when I try to set DataPropertyName field. Datagridview control still binds to the old dataset even though the dataset has been changed.
The only way Ive found to update the datagridview is to delete the dataset and create a new one base on the updated database. Is there a way to update the dataset automatically without deleting the old dataset?
Thanks in advance
|
|
|
|
|
in many times after change fileds in dataset, you need to configure all its dataadapter querys (Fill is first) again. in other word you just open each query and then closed it.
|
|
|
|
|
Hi, i have a form with a panel. In this panel, it allow the shapes drawn (using graphicspath) to be drag and drop. However, there is the flickering problem whenever i drag any of the shapes. i tried to use the SetStyle function like below:
public class DoubleBufferPanel : Panel<br />
{<br />
public DoubleBufferPanel()<br />
{<br />
this.SetStyle(ControlStyles.DoubleBuffer |<br />
ControlStyles.UserPaint |ControlStyles.AllPaintingInWmPaint,<br />
true);<br />
<br />
this.UpdateStyles();<br />
}<br />
}
however, it generates the error below:
An unhandled exception of type 'System.ArgumentException' occurred in system.drawing.dll<br />
<br />
Additional information: Invalid parameter used.
Any suggestion on what should i do?
Thanks in advance
|
|
|
|
|
Hello,
I would assume that the exception is not accuring in the classes constructor.
Have you overriden the "OnPaint" method?
Please show us more code and do some debugging!
All the best,
Martin
|
|
|
|
|
Hi,
you must use the line numbers:
- tell Visual to show line numbers (see my sig, tip 1)
- look at the exception's line numbers (see my sig, tip 2)
Good luck !
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
This should be easy, however I keep getting it wrong I'm completely missing something, and google isn't being much help. I guess its to late... not sure.
I need to calculate how many addresses are in a range. So what i'm looking for is a formula to find this.
example i have
10.126.0.1 through 10.127.0.1 i want to know how many addresses are in between the range.
Thanks!
|
|
|
|
|
One presumes you need to take an IP address, split it into four numbers, and check if each of those numbers is within the ranges specified by doing the same to your range IP addresses.
string [] lower = "10.126.0.1".Split(new char [] {'.'} );
string [] upper= "10.127.0.1".Split(new char [] {'.'} );
string [] range = newIp.Split(new char[] {'.'} );
// check here that range contains 4 strings
bool match = true;
for(int i=0;i<4;++i)
{
match &= (int.TryParse(lower[i]) >= int.TryParse(range[i]));
match &= (int.TryParse(upper[i]) <= int.TryParse(range[i]));
}
Something like that would work, but it has levels of nastiness.
1 - it assumes the values coming in are all valid numbers. Using TryParse would add a lot of code, you could add a try/catch ( if it blows up, they can't match ).
2 - it parses range[i] twice, instead of storing it.
But, it should work, and should give you a good starting point. Assuming my assumptions about the rules that define a number being 'in the range' are correct.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
First, I really appreciate the reply!
Your suggestion might work. (i verify the numbers are in range before the program reaches this point so i'm not worried about it being outside) and I have already converted the numbers to an array of 4 integers when its time to calculate the total, however it's quitting time for me now, so i'll have to wait until tomorrow to try it. But in the mean time if anyone else has a suggestion I'd still rather use a formula for the calculation.
|
|
|
|
|
What about
a method like
int GetIPasint(string ipAddress)
{
string [] ip = ipAddress.Split(new char[] { '.' } );
int ipAddress = 0;
for(int i = 0; i < 4; ++i)
{
int portion;
if (int.TryParse(ip[i], out portion)
{
ipAddress += portion << (4-i);
}
}
return ipAddress;
}
Hopefully, that would work to give you values that fall between the ranges. Although, I have some doubts, I have to say, it's worth a shot.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hi,
the number of addresses in the interval[ a.b.c.d , e.f.g.h ]
equals num32(e,f,g,h) - num32(a,b,c,d) + 1
where num32 is the 32-bit number formed by those 4 byte values, as in:
num32(a,b,c,d) = ((((((a<<8)+b)<<8)+c)<<8)+d);
BTW: you may save a few lines of code using IPAddress.GetAddressBytes(),
unfortunately there seems to be no method IPAddress.GetAddressInt32() !
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
WOW exactly what i needed!!!! THANK YOU!!!
|
|
|
|
|
How can I load a XML into a rich text box?
l_szCurrentFile = m_szDirectory + "\\" + m_oTabControl.SelectedTab.Name.ToString();
rtbDoc.LoadFile(l_szCurrentFile, RichTextBoxStreamType.RichNoOleObjs);
I get an invalid file type error with this
|
|
|
|
|
I think your problem is caused by "RichNoOleObjs ." According to the help file, "This value is only valid for use with the SaveFile method of the RichTextBox control."
Try using PlainText (or UnicodePlainText .)
BDF
|
|
|
|
|
<br />
<br />
using (StreamReader sr = new StreamReader(@"N:\test2.xml"))<br />
richTextBox1.Text = sr.ReadToEnd();<br />
<br />
good luck
|
|
|
|